// migrations/20250619_create_tables.js
exports.up = async function (knex) {
  // Create 'users' table
  await knex.schema.createTable('users', (table) => {
    table.increments('id').primary();
    table.string('username', 255).notNullable().unique();
    table.string('password', 255).notNullable();
    table.timestamp('created_at').defaultTo(knex.fn.now());
  });

  // Create 'chargers' table
  await knex.schema.createTable('chargers', (table) => {
    table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()'));

    table
      .integer('user_id')
      .unsigned()
      .notNullable()
      .references('id')
      .inTable('users')
      .onDelete('CASCADE');

    table.string('location', 255).notNullable();
    table.string('status', 50).notNullable().defaultTo('offline');

    // ✅ FIX: firmware manda amps com decimais (ex: 14.2)
    table.decimal('charging_current', 8, 2).notNullable().defaultTo(32);

    // updated_at: usado pelo offline checker
    table.timestamp('updated_at').notNullable().defaultTo(knex.fn.now());

    table.string('mqtt_user', 255).notNullable();
    table.string('mqtt_pass', 255).notNullable();
    table.string('mqtt_topic', 255).notNullable().unique();

    table.timestamp('created_at').defaultTo(knex.fn.now());

    // Total charging time:
    // ✅ Recomendo guardar em segundos (int).
    // Se o teu firmware mandar decimal (minutos/horas), troca para decimal(10,1).
    table.integer('charging_time').notNullable().defaultTo(0);
    // table.decimal('charging_time', 10, 1).notNullable().defaultTo(0);

    // Consumption (kWh)
    table.decimal('consumption', 10, 3).notNullable().defaultTo(0);
    // ↑ aumentei precisão (kWh acumulado pode crescer e 8,2 é curto em alguns cenários)

    // Power (kW)
    table.decimal('power_l1', 10, 3).notNullable().defaultTo(0);
    table.decimal('power_l2', 10, 3).notNullable().defaultTo(0);
    table.decimal('power_l3', 10, 3).notNullable().defaultTo(0);

    // Voltage (V)
    table.decimal('voltage_l1', 10, 3).notNullable().defaultTo(0);
    table.decimal('voltage_l2', 10, 3).notNullable().defaultTo(0);
    table.decimal('voltage_l3', 10, 3).notNullable().defaultTo(0);

    // Current (A)
    table.decimal('current_l1', 10, 3).notNullable().defaultTo(0);
    table.decimal('current_l2', 10, 3).notNullable().defaultTo(0);
    table.decimal('current_l3', 10, 3).notNullable().defaultTo(0);

    // índices úteis (produção)
    table.index(['user_id']);
    table.index(['updated_at']);
    table.index(['status']);
  });

  // Create 'charger_configs' table
  await knex.schema.createTable('charger_configs', (table) => {
    table
      .uuid('charger_id')
      .primary()
      .references('id')
      .inTable('chargers')
      .onDelete('CASCADE');

    table.integer('max_charging_current').notNullable().defaultTo(32);
    table.boolean('require_auth').notNullable().defaultTo(false);
    table.boolean('rcm_enabled').notNullable().defaultTo(false);
    table.integer('temperature_limit').notNullable().defaultTo(60);
    table.timestamp('config_received_at').notNullable().defaultTo(knex.fn.now());
  });

  // Create 'charger_sessions' table
  await knex.schema.createTable('charger_sessions', (table) => {
    table.increments('id').primary();

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

    table.timestamp('started_at').notNullable();
    table.timestamp('ended_at');

    table.decimal('kwh', 10, 3).notNullable().defaultTo(0);
    table.decimal('cost', 12, 2);

    table.timestamp('created_at').defaultTo(knex.fn.now());

    // índices úteis
    table.index(['charger_id']);
    table.index(['started_at']);
    table.index(['ended_at']);
  });
};

exports.down = async function (knex) {
  await knex.schema.dropTableIfExists('charger_sessions');
  await knex.schema.dropTableIfExists('charger_configs');
  await knex.schema.dropTableIfExists('chargers');
  await knex.schema.dropTableIfExists('users');
};
