<?php

use yii\db\Migration;

/**
 * Creates invoices table. Linked only to customer_id (handles B2C, B2B, gym member billing uniformly).
 */
class m240001_000005_create_invoices_table extends Migration
{
    public function safeUp(): void
    {
        $this->createTable('{{%invoices}}', [
            'id'             => $this->primaryKey()->unsigned()->comment('Primary key'),
            'invoice_number' => $this->string(30)->notNull()->unique()->comment('Unique invoice number e.g. INV-2024-0001'),
            'customer_id'    => $this->integer()->unsigned()->notNull()->comment('FK customers'),
            'customer_plan_id' => $this->integer()->unsigned()->null()->comment('FK customer_plans (optional)'),
            'invoice_date'   => $this->date()->notNull()->comment('Invoice issue date'),
            'due_date'       => $this->date()->null()->comment('Payment due date'),
            'subtotal'       => $this->decimal(12, 2)->notNull()->defaultValue(0.00)->comment('Amount before GST'),
            'gst_rate'       => $this->decimal(5, 2)->notNull()->defaultValue(0.00)->comment('GST % applied'),
            'gst_amount'     => $this->decimal(12, 2)->notNull()->defaultValue(0.00)->comment('Calculated GST amount'),
            'discount_amount'=> $this->decimal(12, 2)->notNull()->defaultValue(0.00)->comment('Discount applied'),
            'total_amount'   => $this->decimal(12, 2)->notNull()->defaultValue(0.00)->comment('subtotal + gst - discount'),
            'paid_amount'    => $this->decimal(12, 2)->notNull()->defaultValue(0.00)->comment('Total amount paid'),
            'status'         => "ENUM('unpaid','partial','paid','cancelled') NOT NULL DEFAULT 'unpaid' COMMENT 'Payment status'",
            'notes'          => $this->text()->null()->comment('Internal notes / remarks'),
            'created_by'     => $this->integer()->unsigned()->null()->comment('Admin user ID who created'),
            'created_at'     => $this->integer()->unsigned()->notNull()->comment('Unix timestamp'),
            'updated_at'     => $this->integer()->unsigned()->notNull()->comment('Unix timestamp'),
        ], 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT="Invoices for all customer types"');

        $this->createIndex('idx_inv_customer', '{{%invoices}}', 'customer_id');
        $this->createIndex('idx_inv_status', '{{%invoices}}', 'status');
        $this->createIndex('idx_inv_date', '{{%invoices}}', 'invoice_date');
        $this->createIndex('idx_inv_customer_plan', '{{%invoices}}', 'customer_plan_id');

        $this->addForeignKey('fk_inv_customer', '{{%invoices}}', 'customer_id', '{{%customers}}', 'id', 'CASCADE', 'CASCADE');
        $this->addForeignKey('fk_inv_customer_plan', '{{%invoices}}', 'customer_plan_id', '{{%customer_plans}}', 'id', 'SET NULL', 'CASCADE');
    }

    public function safeDown(): void
    {
        $this->dropForeignKey('fk_inv_customer', '{{%invoices}}');
        $this->dropForeignKey('fk_inv_customer_plan', '{{%invoices}}');
        $this->dropTable('{{%invoices}}');
    }
}
