<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateMasterSpecializedTables extends Migration
{
    public function up(): void
    {
        // --------------------------------------------------------------------
        // 1. TB_RESELLER
        // --------------------------------------------------------------------
        $this->forge->addField([
            'id_reseller' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'nama_reseller' => [
                'type'       => 'VARCHAR',
                'constraint' => 200,
                'null'       => false,
            ],
            'no_hp_reseller' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
                'null'       => true,
                'default'    => null,
            ],
            'alamat_reseller' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'harga_default_reseller' => [
                'type'       => 'DECIMAL',
                'constraint' => '15,2',
                'null'       => true,
                'default'    => '0.00',
            ],
            'status_reseller' => [
                'type'       => 'ENUM',
                'constraint' => ['Aktif', 'Non-Aktif'],
                'null'       => false,
                'default'    => 'Aktif',
            ],
            'created_at' => [
                'type'    => 'DATETIME',
                'null'    => true,
                'default' => null,
            ],
            'updated_at' => [
                'type'    => 'DATETIME',
                'null'    => true,
                'default' => null,
            ],
        ]);

        $this->forge->addKey('id_reseller', true);
        $this->forge->createTable('tb_reseller', true);

        $this->db->query("
            ALTER TABLE `tb_reseller`
            MODIFY `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
            MODIFY `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ");

        // --------------------------------------------------------------------
        // 2. TB_MAKLON
        // --------------------------------------------------------------------
        $this->forge->addField([
            'id_maklon' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'nama_maklon' => [
                'type'       => 'VARCHAR',
                'constraint' => 200,
                'null'       => false,
            ],
            'no_hp_maklon' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
                'null'       => true,
                'default'    => null,
            ],
            'alamat_maklon' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'harga_default_maklon' => [
                'type'       => 'DECIMAL',
                'constraint' => '15,2',
                'null'       => true,
                'default'    => '0.00',
            ],
            'status_maklon' => [
                'type'       => 'ENUM',
                'constraint' => ['Aktif', 'Non-Aktif'],
                'null'       => false,
                'default'    => 'Aktif',
            ],
            'created_at' => [
                'type'    => 'DATETIME',
                'null'    => true,
                'default' => null,
            ],
        ]);

        $this->forge->addKey('id_maklon', true);
        $this->forge->createTable('tb_maklon');

        $this->db->query("
            ALTER TABLE `tb_maklon`
            MODIFY `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
        ");

        // --------------------------------------------------------------------
        // 3. TB_MARKETPLACE
        // --------------------------------------------------------------------
        $this->forge->addField([
            'id_marketplace' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'nama_marketplace' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => false,
            ],
            'status_marketplace' => [
                'type'       => 'ENUM',
                'constraint' => ['Aktif', 'Non-Aktif'],
                'null'       => false,
                'default'    => 'Aktif',
            ],
            'created_at' => [
                'type'    => 'DATETIME',
                'null'    => true,
                'default' => null,
            ],
            'updated_at' => [
                'type'    => 'DATETIME',
                'null'    => true,
                'default' => null,
            ],
        ]);


        $this->forge->addKey('id_marketplace', true);
        $this->forge->createTable('tb_marketplace', true);

        $this->db->query("
            ALTER TABLE `tb_marketplace`
            MODIFY `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
            MODIFY `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ");

        // --------------------------------------------------------------------
        // 4. ALTER tabel existing
        // --------------------------------------------------------------------

    }

    public function down(): void
    {
        try {
            $this->db->query("ALTER TABLE `tb_karyawan` DROP COLUMN `status_karyawan`");
        } catch (\Exception $e) {
        }
        try {
            $this->db->query("ALTER TABLE `tb_bahan`    DROP COLUMN `status_bahan`");
        } catch (\Exception $e) {
        }
        try {
            $this->db->query("ALTER TABLE `tb_produk`   DROP COLUMN `status_produk`");
        } catch (\Exception $e) {
        }

        $this->forge->dropTable('tb_marketplace', true);
        $this->forge->dropTable('tb_maklon',      true);
        $this->forge->dropTable('tb_reseller',    true);
    }
}
