// 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 with new fields
  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');
    table.integer('charging_current').notNullable().defaultTo(32);
    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());

    // Add power and current for 3 phases, voltage and other new fields
    table.integer('charging_time').notNullable().defaultTo(0); // Total charging time
    table.decimal('consumption', 8, 2).notNullable().defaultTo(0); // Consumption (kWh)
    
    // Power for 3 phases (L1, L2, L3)
    table.decimal('power_l1', 8, 2).notNullable().defaultTo(0);
    table.decimal('power_l2', 8, 2).notNullable().defaultTo(0);
    table.decimal('power_l3', 8, 2).notNullable().defaultTo(0);

    // Voltage for 3 phases (L1, L2, L3)
    table.decimal('voltage_l1', 8, 2).notNullable().defaultTo(0);
    table.decimal('voltage_l2', 8, 2).notNullable().defaultTo(0);
    table.decimal('voltage_l3', 8, 2).notNullable().defaultTo(0);

    // Current for 3 phases (L1, L2, L3)
    table.decimal('current_l1', 8, 2).notNullable().defaultTo(0);
    table.decimal('current_l2', 8, 2).notNullable().defaultTo(0);
    table.decimal('current_l3', 8, 2).notNullable().defaultTo(0);
  });

  // 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', 8, 2).notNullable().defaultTo(0);
    table.decimal('cost', 10, 2);
    table.timestamp('created_at').defaultTo(knex.fn.now());
  });
};

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');
};
