<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateOneCoreSchema extends Migration
{
    public function up(): void
    {
        $this->users();
        $this->roles();
        $this->permissions();
        $this->modules();
        $this->auditLogs();
        $this->aiRequests();
        $this->pages();
        $this->databaseConnections();
        $this->license();
        $this->settings();
    }

    public function down(): void
    {
        foreach ([
            'core_usage_counters',
            'core_license',
            'core_database_connections',
            'core_page_widgets',
            'core_pages',
            'core_ai_requests',
            'core_audit_logs',
            'core_modules',
            'core_role_permissions',
            'core_user_roles',
            'core_permissions',
            'core_roles',
            'core_users',
            'core_settings',
        ] as $table) {
            $this->forge->dropTable($table, true);
        }
    }

    private function users(): void
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'email' => ['type' => 'VARCHAR', 'constraint' => 190],
            'password_hash' => ['type' => 'VARCHAR', 'constraint' => 255],
            'firstname' => ['type' => 'VARCHAR', 'constraint' => 120, 'null' => true],
            'lastname' => ['type' => 'VARCHAR', 'constraint' => 120, 'null' => true],
            'status' => ['type' => 'VARCHAR', 'constraint' => 30, 'default' => 'active'],
            'last_login_at' => ['type' => 'DATETIME', 'null' => true],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addUniqueKey('email');
        $this->forge->createTable('core_users');
    }

    private function roles(): void
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'name' => ['type' => 'VARCHAR', 'constraint' => 80],
            'label' => ['type' => 'VARCHAR', 'constraint' => 120],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addUniqueKey('name');
        $this->forge->createTable('core_roles');

        $this->forge->addField([
            'user_id' => ['type' => 'INT', 'unsigned' => true],
            'role_id' => ['type' => 'INT', 'unsigned' => true],
        ]);
        $this->forge->addPrimaryKey(['user_id', 'role_id']);
        $this->forge->createTable('core_user_roles');
    }

    private function permissions(): void
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'name' => ['type' => 'VARCHAR', 'constraint' => 120],
            'label' => ['type' => 'VARCHAR', 'constraint' => 160],
            'group_name' => ['type' => 'VARCHAR', 'constraint' => 80],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addUniqueKey('name');
        $this->forge->createTable('core_permissions');

        $this->forge->addField([
            'role_id' => ['type' => 'INT', 'unsigned' => true],
            'permission_id' => ['type' => 'INT', 'unsigned' => true],
        ]);
        $this->forge->addPrimaryKey(['role_id', 'permission_id']);
        $this->forge->createTable('core_role_permissions');
    }

    private function modules(): void
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'name' => ['type' => 'VARCHAR', 'constraint' => 80],
            'label' => ['type' => 'VARCHAR', 'constraint' => 120],
            'version' => ['type' => 'VARCHAR', 'constraint' => 40],
            'enabled' => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 1],
            'config_json' => ['type' => 'TEXT', 'null' => true],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addUniqueKey('name');
        $this->forge->createTable('core_modules');
    }

    private function auditLogs(): void
    {
        $this->forge->addField([
            'id' => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'user_id' => ['type' => 'INT', 'unsigned' => true, 'null' => true],
            'action' => ['type' => 'VARCHAR', 'constraint' => 120],
            'entity_type' => ['type' => 'VARCHAR', 'constraint' => 120, 'null' => true],
            'entity_id' => ['type' => 'VARCHAR', 'constraint' => 120, 'null' => true],
            'description' => ['type' => 'TEXT', 'null' => true],
            'ip_address' => ['type' => 'VARCHAR', 'constraint' => 64, 'null' => true],
            'user_agent' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addKey('user_id');
        $this->forge->createTable('core_audit_logs');
    }

    private function aiRequests(): void
    {
        $this->forge->addField([
            'id' => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'user_id' => ['type' => 'INT', 'unsigned' => true, 'null' => true],
            'provider' => ['type' => 'VARCHAR', 'constraint' => 60, 'default' => 'openai'],
            'model' => ['type' => 'VARCHAR', 'constraint' => 80, 'null' => true],
            'prompt' => ['type' => 'TEXT'],
            'response' => ['type' => 'MEDIUMTEXT', 'null' => true],
            'tokens_input' => ['type' => 'INT', 'unsigned' => true, 'null' => true],
            'tokens_output' => ['type' => 'INT', 'unsigned' => true, 'null' => true],
            'cost_estimate' => ['type' => 'DECIMAL', 'constraint' => '10,6', 'null' => true],
            'status' => ['type' => 'VARCHAR', 'constraint' => 30, 'default' => 'pending'],
            'error_message' => ['type' => 'TEXT', 'null' => true],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addKey('user_id');
        $this->forge->createTable('core_ai_requests');
    }

    private function pages(): void
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'module' => ['type' => 'VARCHAR', 'constraint' => 80],
            'title' => ['type' => 'VARCHAR', 'constraint' => 190],
            'slug' => ['type' => 'VARCHAR', 'constraint' => 160],
            'description' => ['type' => 'TEXT', 'null' => true],
            'config_json' => ['type' => 'MEDIUMTEXT', 'null' => true],
            'created_by' => ['type' => 'INT', 'unsigned' => true, 'null' => true],
            'status' => ['type' => 'VARCHAR', 'constraint' => 30, 'default' => 'draft'],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addKey(['module', 'slug']);
        $this->forge->createTable('core_pages');

        $this->forge->addField([
            'id' => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'page_id' => ['type' => 'INT', 'unsigned' => true],
            'widget_type' => ['type' => 'VARCHAR', 'constraint' => 60],
            'title' => ['type' => 'VARCHAR', 'constraint' => 190],
            'position' => ['type' => 'INT', 'unsigned' => true, 'default' => 0],
            'config_json' => ['type' => 'MEDIUMTEXT', 'null' => true],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addKey('page_id');
        $this->forge->createTable('core_page_widgets');
    }

    private function databaseConnections(): void
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'name' => ['type' => 'VARCHAR', 'constraint' => 120],
            'driver' => ['type' => 'VARCHAR', 'constraint' => 40, 'default' => 'MySQLi'],
            'host' => ['type' => 'VARCHAR', 'constraint' => 190],
            'port' => ['type' => 'INT', 'unsigned' => true, 'default' => 3306],
            'database_name' => ['type' => 'VARCHAR', 'constraint' => 190],
            'username' => ['type' => 'VARCHAR', 'constraint' => 190],
            'encrypted_password' => ['type' => 'TEXT', 'null' => true],
            'charset' => ['type' => 'VARCHAR', 'constraint' => 40, 'default' => 'utf8mb4'],
            'status' => ['type' => 'VARCHAR', 'constraint' => 30, 'default' => 'active'],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->createTable('core_database_connections');
    }

    private function license(): void
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'license_key' => ['type' => 'VARCHAR', 'constraint' => 80],
            'product' => ['type' => 'VARCHAR', 'constraint' => 80, 'default' => 'one-core'],
            'plan' => ['type' => 'VARCHAR', 'constraint' => 40, 'default' => 'free'],
            'status' => ['type' => 'VARCHAR', 'constraint' => 30, 'default' => 'active'],
            'domain' => ['type' => 'VARCHAR', 'constraint' => 190, 'null' => true],
            'expires_at' => ['type' => 'DATETIME', 'null' => true],
            'max_users' => ['type' => 'INT', 'unsigned' => true, 'default' => 3],
            'max_modules' => ['type' => 'INT', 'unsigned' => true, 'default' => 2],
            'max_ai_requests_month' => ['type' => 'INT', 'unsigned' => true, 'default' => 100],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addUniqueKey('license_key');
        $this->forge->createTable('core_license');

        $this->forge->addField([
            'id' => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'metric' => ['type' => 'VARCHAR', 'constraint' => 120],
            'value' => ['type' => 'INT', 'unsigned' => true, 'default' => 0],
            'period' => ['type' => 'VARCHAR', 'constraint' => 20],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addUniqueKey(['metric', 'period']);
        $this->forge->createTable('core_usage_counters');
    }

    private function settings(): void
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'setting_key' => ['type' => 'VARCHAR', 'constraint' => 120],
            'setting_value' => ['type' => 'TEXT', 'null' => true],
            'setting_group' => ['type' => 'VARCHAR', 'constraint' => 80, 'default' => 'general'],
            'is_encrypted' => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addUniqueKey('setting_key');
        $this->forge->createTable('core_settings');
    }
}
