<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateTbPenjualanTable extends Migration
{
    public function up(): void
    {
        /*
        |--------------------------------------------------------------------------
        | tb_penjualan
        |--------------------------------------------------------------------------
        */

        $this->forge->addField([
            'id_penjualan' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],

            'kode_penjualan' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
            ],

            'tanggal_penjualan' => [
                'type' => 'DATE',
            ],

            'jenis_penjualan' => [
                'type'       => 'ENUM',
                'constraint' => ['Ecer', 'Reseller', 'Maklon'],
                'default'    => 'Ecer',
            ],

            'id_reseller' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],

            'id_maklon' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],

            'total_penjualan' => [
                'type'       => 'DECIMAL',
                'constraint' => '15,2',
                'default'    => '0.00',
            ],

            'catatan_penjualan' => [
                'type' => 'TEXT',
                'null' => true,
            ],

            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],

            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id_penjualan', true);
        $this->forge->addUniqueKey('kode_penjualan');

        $this->forge->addKey('tanggal_penjualan');
        $this->forge->addKey('jenis_penjualan');
        $this->forge->addKey('id_reseller');
        $this->forge->addKey('id_maklon');

        /*
        |--------------------------------------------------------------------------
        | Foreign Key
        |--------------------------------------------------------------------------
        */

        $this->forge->addForeignKey(
            'id_reseller',
            'tb_reseller',
            'id_reseller',
            'SET NULL',
            'CASCADE'
        );

        $this->forge->addForeignKey(
            'id_maklon',
            'tb_maklon',
            'id_maklon',
            'SET NULL',
            'CASCADE'
        );

        $this->forge->createTable('tb_penjualan', true, [
            'ENGINE' => 'InnoDB',
        ]);

        /*
        |--------------------------------------------------------------------------
        | tb_penjualan_item
        |--------------------------------------------------------------------------
        */

        $this->forge->addField([
            'id_penjualan_item' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],

            'id_penjualan' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],

            'id_produk' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],

            'qty' => [
                'type'       => 'INT',
                'constraint' => 11,
                'default'    => 1,
            ],

            'harga_jual' => [
                'type'       => 'DECIMAL',
                'constraint' => '15,2',
                'default'    => '0.00',
            ],

            'subtotal' => [
                'type'       => 'DECIMAL',
                'constraint' => '15,2',
                'default'    => '0.00',
            ],

            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id_penjualan_item', true);

        $this->forge->addKey('id_penjualan');
        $this->forge->addKey('id_produk');

        $this->forge->addForeignKey(
            'id_penjualan',
            'tb_penjualan',
            'id_penjualan',
            'CASCADE',
            'CASCADE'
        );

        $this->forge->addForeignKey(
            'id_produk',
            'tb_produk',
            'id_produk',
            'RESTRICT',
            'RESTRICT'
        );

        $this->forge->createTable('tb_penjualan_item', true, [
            'ENGINE' => 'InnoDB',
        ]);
    }

    public function down(): void
    {
        $this->forge->dropTable('tb_penjualan_item', true);
        $this->forge->dropTable('tb_penjualan', true);
    }
}