<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddOrderFieldsAndOrderItems extends Migration
{
    public function up(): void
    {
        $this->forge->addColumn('orders', [
            'location_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
                'after'      => 'driver_id',
            ],
            'order_type' => [
                'type'       => 'ENUM',
                'constraint' => ['gas', 'fuel', 'shop', 'rescue'],
                'default'    => 'gas',
                'null'       => true,
                'after'      => 'service_id',
            ],
            'order_meta' => [
                'type' => 'JSON',
                'null' => true,
                'after' => 'order_type',
            ],
            'rescue_service_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
                'after'      => 'order_meta',
            ],
        ]);
        $this->db->query('ALTER TABLE orders MODIFY service_id INT(11) UNSIGNED NULL');
        $this->db->query('ALTER TABLE orders ADD CONSTRAINT fk_orders_rescue_service FOREIGN KEY (rescue_service_id) REFERENCES rescue_services(id) ON DELETE SET NULL ON UPDATE CASCADE');
        $this->db->query('ALTER TABLE orders ADD CONSTRAINT fk_orders_location FOREIGN KEY (location_id) REFERENCES locations(id) ON DELETE SET NULL ON UPDATE CASCADE');

        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'order_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'inventory_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'quantity' => [
                'type'       => 'INT',
                'constraint' => 11,
                'default'    => 1,
            ],
            'unit_price' => [
                'type'       => 'DECIMAL',
                'constraint' => '10,2',
                'default'    => 0.00,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey('order_id');
        $this->forge->addForeignKey('order_id', 'orders', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('inventory_id', 'inventory', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('order_items');
    }

    public function down(): void
    {
        $this->forge->dropTable('order_items');
        $this->db->query('ALTER TABLE orders DROP FOREIGN KEY fk_orders_rescue_service');
        $this->db->query('ALTER TABLE orders DROP FOREIGN KEY fk_orders_location');
        $this->forge->dropColumn('orders', ['location_id', 'order_type', 'order_meta', 'rescue_service_id']);
        $this->db->query('ALTER TABLE orders MODIFY service_id INT(11) UNSIGNED NOT NULL');
    }
}
