<?php

use Licenzo\Framework\Database\Migration\Migration;

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

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

    public function up()
    {
        return $this->create($this->table, [
            'id'                    => 'BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY',

            // Provider details
            'provider'              => 'VARCHAR(50) NOT NULL', // stripe, paypal, woocommerce, manual
            'provider_id'           => 'BIGINT UNSIGNED NULL',
            'provider_charge_id'    => 'VARCHAR(255) NULL', // Charge ID from provider

            // Order details
            'order_id'              => 'BIGINT UNSIGNED NULL',
            'order_type'            => 'VARCHAR(50) NULL', // woocommerce, custom

            // Transaction details
            'transaction_type'      => 'VARCHAR(50) NOT NULL', // charge, refund, purchase, renewal, upgrade, downgrade
            'subscription_id'       => 'BIGINT UNSIGNED NOT NULL',
            'customer_id'           => 'BIGINT UNSIGNED NOT NULL',

            // Payment method
            'card_last_4'           => 'VARCHAR(4) NULL',
            'card_brand'            => 'VARCHAR(50) NULL',
            'payment_method'        => 'VARCHAR(50) NULL', // card, bank_account, paypal
            'payment_mode'          => 'VARCHAR(50) NULL', // live, test
            'payment_method_type'   => 'VARCHAR(50) NULL',

            // Amount
            'status'                => 'VARCHAR(50) NOT NULL', // success, failed, pending, refunded
            'currency'              => 'VARCHAR(10) NOT NULL DEFAULT "USD"',
            'total'                 => 'BIGINT NOT NULL',
            'rate'                  => 'DECIMAL(10,4) DEFAULT 1.0000',

            // Additional info
            'uuid'                  => 'CHAR(36) NULL',
            'description'           => 'TEXT NULL',
            'meta'                  => 'LONGTEXT NULL', // JSON

            // Failure tracking
            'failure_reason'        => 'TEXT NULL',
            'failure_code'          => 'VARCHAR(100) NULL',

            // Refund tracking
            'refund_reason'         => 'TEXT NULL',

            // URLs
            'invoice_url'           => 'VARCHAR(500) NULL',
            'receipt_url'           => 'VARCHAR(500) 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']],
            ['type' => 'KEY', 'name' => 'idx_customer_id', 'columns' => ['customer_id']],
        ]);
    }

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

