<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Control DB only. Holds tenant metadata (which hotel, which physical
 * database, branding for invoices/UI chrome) — never any hotel operational
 * data. Runs against the 'default' DB group via the normal `spark migrate`.
 */
class CreateTenantsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'hotel_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'slug' => [
                'type'       => 'VARCHAR',
                'constraint' => 63,
                'comment'    => 'Subdomain segment, e.g. "demo" for demo.maxyields.com',
            ],
            'db_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 64,
                'comment'    => 'Physical MySQL database name, e.g. tenant_demo',
            ],
            'db_user' => [
                'type'       => 'VARCHAR',
                'constraint' => 64,
                'null'       => true,
                'comment'    => 'Informational for Phase 1 (a shared app DB user connects to every tenant DB); lets a tenant be moved to dedicated DB credentials later without a schema change.',
            ],
            'status' => [
                'type'       => 'ENUM',
                'constraint' => ['active', 'suspended'],
                'default'    => 'active',
            ],
            'logo_path' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => true,
            ],
            'primary_color' => [
                'type'       => 'VARCHAR',
                'constraint' => 7,
                'null'       => true,
                'comment'    => 'Hex color, e.g. #1a5f3f',
            ],
            'secondary_color' => [
                'type'       => 'VARCHAR',
                'constraint' => 7,
                'null'       => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('slug');
        $this->forge->addUniqueKey('db_name');
        $this->forge->createTable('tenants');
    }

    public function down()
    {
        $this->forge->dropTable('tenants', true);
    }
}
