<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateOrderProductionSchedules extends Migration
{
    public function up()
    {
        if ($this->db->tableExists('order_production_schedules')) {
            return;
        }

        $this->forge->addField([
            'id_order_production_schedule' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true,
            ],
            'id_order' => [
                'type' => 'INT',
                'constraint' => 11,
            ],
            'scheduled_month' => [
                'type' => 'DATE',
            ],
            'quantity' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id_order_production_schedule', true);
        $this->forge->addKey(['id_order', 'scheduled_month']);
        $this->forge->addForeignKey('id_order', 'orders', 'id_order', 'CASCADE', 'CASCADE');
        $this->forge->createTable('order_production_schedules');
    }

    public function down()
    {
        if ($this->db->tableExists('order_production_schedules')) {
            $this->forge->dropTable('order_production_schedules', true);
        }
    }
}
