<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateAllTables extends Migration
{
    public function up()
    {
        // Customer
        $this->forge->addField([
            'id_customer' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'nama' => ['type' => 'VARCHAR', 'constraint' => 100],
            'alamat' => ['type' => 'TEXT', 'null' => true],
            'no_hp' => ['type' => 'VARCHAR', 'constraint' => 25],
            'username' => ['type' => 'VARCHAR', 'constraint' => 100, 'unique' => true],
            'password' => ['type' => 'VARCHAR', 'constraint' => 255],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id_customer', true);
        $this->forge->createTable('tb_customer');

        // Pegawai
        $this->forge->addField([
            'id_pegawai' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'nama' => ['type' => 'VARCHAR', 'constraint' => 100],
            'username' => ['type' => 'VARCHAR', 'constraint' => 250, 'unique' => true],
            'password' => ['type' => 'VARCHAR', 'constraint' => 250],
            'role' => ['type' => 'VARCHAR', 'constraint' => 100],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id_pegawai', true);
        $this->forge->createTable('pegawai');

        // Model Custom
        $this->forge->addField([
            'id_model' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'nama_model' => ['type' => 'VARCHAR', 'constraint' => 100],
            'deskripsi' => ['type' => 'VARCHAR', 'constraint' => 250, 'null' => true],
            'gambar_model' => ['type' => 'VARCHAR', 'constraint' => 250, 'null' => true],
            'harga' => ['type' => 'DOUBLE', 'default' => 0],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id_model', true);
        $this->forge->createTable('model_custom');

        // Jenis Kain
        $this->forge->addField([
            'id_jns_kain' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'nama_kain' => ['type' => 'VARCHAR', 'constraint' => 100],
            'warna' => ['type' => 'VARCHAR', 'constraint' => 100, 'null' => true],
            'stock' => ['type' => 'INT', 'default' => 0],
            'foto' => ['type' => 'VARCHAR', 'constraint' => 250, 'null' => true],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id_jns_kain', true);
        $this->forge->createTable('jenis_kain');

        // Pesanan Custom
        $this->forge->addField([
            'id_permintaan' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'id_customer' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'id_model' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'id_jns_kain' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'ukuran' => ['type' => 'VARCHAR', 'constraint' => 50, 'null' => true],
            'warna' => ['type' => 'VARCHAR', 'constraint' => 100, 'null' => true],
            'tanggal_pemesanan' => ['type' => 'DATE'],
            'detail_custom' => ['type' => 'TEXT', 'null' => true],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id_permintaan', true);
        $this->forge->addForeignKey('id_customer', 'tb_customer', 'id_customer', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('id_model', 'model_custom', 'id_model', 'SET NULL', 'CASCADE');
        $this->forge->addForeignKey('id_jns_kain', 'jenis_kain', 'id_jns_kain', 'SET NULL', 'CASCADE');
        $this->forge->createTable('permintaan_custom');

        // Permintaan Perubahan
        $this->forge->addField([
            'id_perubahan' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'id_permintaan' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'detail_perubahan' => ['type' => 'TEXT'],
            'tanggal_perubahan' => ['type' => 'DATE'],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id_perubahan', true);
        $this->forge->addForeignKey('id_permintaan', 'permintaan_custom', 'id_permintaan', 'CASCADE', 'CASCADE');
        $this->forge->createTable('permintaan_perubahan');

        // Riwayat Perubahan
        $this->forge->addField([
            'id_riwayat' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'id_perubahan' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'tanggal' => ['type' => 'DATE'],
            'keterangan' => ['type' => 'VARCHAR', 'constraint' => 250, 'null' => true],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id_riwayat', true);
        $this->forge->addForeignKey('id_perubahan', 'permintaan_perubahan', 'id_perubahan', 'CASCADE', 'CASCADE');
        $this->forge->createTable('riwayat_perubahan');

        // Pembayaran
        $this->forge->addField([
            'id_pembayaran' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'id_permintaan' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'tanggal_bayar' => ['type' => 'DATE'],
            'jumlah_bayar' => ['type' => 'DOUBLE'],
            'status' => ['type' => 'ENUM("DP","Lunas")', 'default' => 'DP'],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id_pembayaran', true);
        $this->forge->addForeignKey('id_permintaan', 'permintaan_custom', 'id_permintaan', 'CASCADE', 'CASCADE');
        $this->forge->createTable('pembayaran');

        // Laporan
        $this->forge->addField([
            'id_laporan' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'periode' => ['type' => 'VARCHAR', 'constraint' => 100],
            'total_transaksi' => ['type' => 'INT', 'default' => 0],
            'total_pendapatan' => ['type' => 'INT', 'default' => 0],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id_laporan', true);
        $this->forge->createTable('laporan');
    }

    public function down()
    {
        $this->forge->dropTable('laporan');
        $this->forge->dropTable('pembayaran');
        $this->forge->dropTable('riwayat_perubahan');
        $this->forge->dropTable('permintaan_perubahan');
        $this->forge->dropTable('permintaan_custom');
        $this->forge->dropTable('jenis_kain');
        $this->forge->dropTable('model_custom');
        $this->forge->dropTable('pegawai');
        $this->forge->dropTable('tb_customer');
    }
}
