<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateInvoices extends Migration
{
    public function up()
    {
        $this->forge->addField([
            "id" => [
                "type" => "INT",
                "unsigned" => true,
                "auto_increment" => true,
            ],
            "client_id" => ["type" => "INT", "unsigned" => true],
            "quotation_id" => ["type" => "INT", "unsigned" => true],
            "invoice_number" => ["type" => "VARCHAR", "constraint" => 50],
            "invoice_date" => ["type" => "DATE"],
            "due_date" => ["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("invoice_number");
        $this->forge->addUniqueKey("quotation_id");
        $this->forge->addKey("client_id");
        $this->forge->addKey("status");
        $this->forge->addForeignKey(
            "client_id",
            "clients",
            "id",
            "CASCADE",
            "RESTRICT",
        );
        $this->forge->addForeignKey(
            "quotation_id",
            "quotations",
            "id",
            "CASCADE",
            "RESTRICT",
        );
        $this->forge->createTable("invoices", true, ["ENGINE" => "InnoDB"]);

        $this->forge->addField([
            "id" => [
                "type" => "INT",
                "unsigned" => true,
                "auto_increment" => true,
            ],
            "invoice_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("invoice_id");
        $this->forge->addForeignKey(
            "invoice_id",
            "invoices",
            "id",
            "CASCADE",
            "CASCADE",
        );
        $this->forge->createTable("invoice_items", true, [
            "ENGINE" => "InnoDB",
        ]);
    }

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