<?php

use Licenzo\Framework\Database\Migration\Migration;

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

return new class extends Migration {
    protected $table = 'licenzo_invoices';

    public function up()
    {
        return $this->create($this->table, [
            'id'                  => 'BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY',
            'uuid'                => 'CHAR(36) NOT NULL',
            'subscription_id'     => 'BIGINT UNSIGNED NOT NULL',
            'customer_id'         => 'BIGINT UNSIGNED NOT NULL',
            'provider'            => 'VARCHAR(50) NOT NULL',
            'provider_invoice_id' => 'VARCHAR(255) NULL',
            'invoice_number'      => 'VARCHAR(100) NOT NULL',
            'billing_cycle_no'    => 'INT UNSIGNED NOT NULL DEFAULT 1',
            'installment_index'   => 'INT UNSIGNED NOT NULL DEFAULT 1',
            'installment_total'   => 'INT UNSIGNED NOT NULL DEFAULT 1',
            'collection_method'   => 'VARCHAR(50) NULL',
            'status'              => 'VARCHAR(50) NOT NULL DEFAULT "draft"',
            'currency'            => 'VARCHAR(10) NOT NULL DEFAULT "USD"',
            'subtotal'            => 'BIGINT NOT NULL DEFAULT 0',
            'tax_total'           => 'BIGINT NOT NULL DEFAULT 0',
            'total'               => 'BIGINT NOT NULL DEFAULT 0',
            'amount_due'          => 'BIGINT NOT NULL DEFAULT 0',
            'amount_paid'         => 'BIGINT NOT NULL DEFAULT 0',
            'period_start'        => 'TIMESTAMP NULL',
            'period_end'          => 'TIMESTAMP NULL',
            'due_at'              => 'TIMESTAMP NULL',
            'paid_at'             => 'TIMESTAMP NULL',
            'snapshot'            => 'LONGTEXT NULL',
            'meta'                => 'LONGTEXT NULL',
            'created_at'          => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
            'updated_at'          => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
        ], [
            ['type' => 'KEY', 'name' => 'idx_subscription_id', 'columns' => ['subscription_id']],
        ]);
    }

    public function down()
    {
        return $this->drop($this->table);
    }
};
