exports.up = async function (knex) {
  const exists = await knex.schema.hasTable('charger_schedules');
  if (exists) return;

  return knex.schema.createTable('charger_schedules', (t) => {
    t.uuid('id')
      .primary()
      .defaultTo(knex.raw('gen_random_uuid()'));

    t.uuid('charger_id')
      .notNullable()
      .references('id')
      .inTable('chargers')
      .onDelete('CASCADE');

    t.string('start', 5).notNullable();
    t.string('end', 5).notNullable();

    t.enu('repeat', ['everyday', 'weekdays', 'weekends'])
      .notNullable()
      .defaultTo('everyday');

    t.timestamp('created_at').defaultTo(knex.fn.now());
  });
};

exports.down = function (knex) {
  return knex.schema.dropTableIfExists('charger_schedules');
};
