<?php

use yii\db\Migration;

/**
 * Migration for Portal People Credit Notes Module
 * 
 * Creates credit_notes table for tracking credit notes across entities
 */
class m260311_000001_create_credit_notes_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        // Create credit_notes table
        $this->createTable('credit_notes', [
            'id' => $this->primaryKey(),
            'entity_id' => $this->integer()->notNull()->comment('ID of entity (invoice, subscription, etc)'),
            'entity_type' => $this->string(255)->notNull()->comment('Fully qualified class name of entity'),
            'credit_note_number' => $this->string(100)->unique()->comment('Credit note number'),
            'amount' => $this->decimal(19, 2)->notNull()->comment('Credit amount'),
            'reason' => $this->string(255)->notNull()->comment('Reason for credit note'),
            'status' => $this->string(50)->defaultValue('Draft')->comment('Draft|Issued|Cancelled'),
            'created_at' => $this->datetime()->notNull()->comment('Creation timestamp'),
            'created_by' => $this->string(255)->comment('User who created credit note'),
            'notes' => $this->text()->comment('Additional notes'),
        ]);

        // Indexes for credit_notes table
        $this->createIndex('idx_credit_notes_entity', 'credit_notes', ['entity_id', 'entity_type']);
        $this->createIndex('idx_credit_notes_status', 'credit_notes', 'status');
        $this->createIndex('idx_credit_notes_created_at', 'credit_notes', 'created_at');
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable('credit_notes');
    }
}
