<?php

use Licenzo\Framework\Database\Migration\Migration;

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

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

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

            'provider'              => 'VARCHAR(100) NULL',
            'provider_id'           => 'VARCHAR(255) NULL',

            // Cached values for quick access
            'product_name'      => 'VARCHAR(255) NOT NULL',
            'variation_name'    => 'VARCHAR(255) NULL',
            'currency'          => 'VARCHAR(10) NOT NULL DEFAULT "USD"',
            'price'             => 'BIGINT NOT NULL',
            'quantity'          => 'INT UNSIGNED DEFAULT 1',

            // Complete snapshots (JSON) - Protects from product updates
            'product_data'      => 'LONGTEXT NULL COMMENT "Full product data at subscription time"',
            'variation_data'    => 'LONGTEXT NULL COMMENT "Full variation data at subscription time"',

            '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);
    }
};

