<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateQuotations extends Migration
{
    public function up()
    {
        $this->forge->addField([
            "id" => [
                "type" => "INT",
                "unsigned" => true,
                "auto_increment" => true,
            ],
            "client_id" => ["type" => "INT", "unsigned" => true],
            "quotation_number" => ["type" => "VARCHAR", "constraint" => 50],
            "quotation_date" => ["type" => "DATE"],
            "valid_until" => ["type" => "DATE", "null" => true],
            "status" => [
                "type" => "VARCHAR",
                "constraint" => 20,
                "default" => "draft",
            ],
            "notes" => ["type" => "TEXT", "null" => true],
            "subtotal" => [
                "type" => "DECIMAL",
                "constraint" => "12,2",
                "default" => "0.00",
            ],
            "total" => [
                "type" => "DECIMAL",
                "constraint" => "12,2",
                "default" => "0.00",
            ],
            "created_at" => ["type" => "DATETIME", "null" => true],
            "updated_at" => ["type" => "DATETIME", "null" => true],
        ]);
        $this->forge->addKey("id", true);
        $this->forge->addUniqueKey("quotation_number");
        $this->forge->addKey("client_id");
        $this->forge->addKey("status");
        $this->forge->addKey("created_at");
        $this->forge->addForeignKey(
            "client_id",
            "clients",
            "id",
            "RESTRICT",
            "CASCADE",
        );
        $this->forge->createTable("quotations", true);

        $this->forge->addField([
            "id" => [
                "type" => "INT",
                "unsigned" => true,
                "auto_increment" => true,
            ],
            "quotation_id" => ["type" => "INT", "unsigned" => true],
            "item_name" => ["type" => "VARCHAR", "constraint" => 150],
            "description" => ["type" => "TEXT", "null" => true],
            "quantity" => ["type" => "DECIMAL", "constraint" => "10,2"],
            "price" => ["type" => "DECIMAL", "constraint" => "12,2"],
            "line_total" => ["type" => "DECIMAL", "constraint" => "12,2"],
            "sort_order" => [
                "type" => "INT",
                "unsigned" => true,
                "default" => 0,
            ],
            "created_at" => ["type" => "DATETIME", "null" => true],
            "updated_at" => ["type" => "DATETIME", "null" => true],
        ]);
        $this->forge->addKey("id", true);
        $this->forge->addKey("quotation_id");
        $this->forge->addForeignKey(
            "quotation_id",
            "quotations",
            "id",
            "CASCADE",
            "CASCADE",
        );
        $this->forge->createTable("quotation_items", true);
    }

    public function down()
    {
        $this->forge->dropTable("quotation_items", true);
        $this->forge->dropTable("quotations", true);
    }
}
