<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class CreateTenants extends AbstractMigration
{
    public function change(): void
    {
        $this->table('tenants', ['id' => 'tenant_id', 'signed' => false])
            ->addColumn('name',         'string',  ['limit' => 120])
            ->addColumn('subdomain',    'string',  ['limit' => 63, 'null' => true])
            ->addColumn('status',       'string',  ['limit' => 20, 'default' => 'active'])
            ->addColumn('contact_email','string',  ['limit' => 120, 'null' => true])
            ->addColumn('contact_phone','string',  ['limit' => 30,  'null' => true])
            ->addColumn('timezone',     'string',  ['limit' => 60,  'default' => 'Africa/Dar_es_Salaam'])
            ->addColumn('logo_path',    'string',  ['limit' => 255, 'null' => true])
            ->addColumn('created_at',   'datetime',['default' => 'CURRENT_TIMESTAMP'])
            ->addColumn('updated_at',   'datetime',['default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP'])
            ->addColumn('deleted_at',   'datetime',['null' => true])
            ->addIndex(['subdomain'], ['unique' => true])
            ->addIndex(['status'])
            ->create();

        // Seed the first tenant — maps 1:1 to the legacy client_id=1 during data migration.
        $this->table('tenants')->insert([
            [
                'name'          => 'Orchid Investment Limited',
                'subdomain'     => 'orchid',
                'status'        => 'active',
                'contact_email' => null,
                'timezone'      => 'Africa/Dar_es_Salaam',
            ],
        ])->saveData();
    }
}
