<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('user_id')->unsigned()->index();
            $table->bigInteger('category_id')->unsigned()->index();
            $table->tinyInteger('status')->default(1)->index();
            $table->text('name')->nullable();
            $table->string('slug')->nullable();
            $table->text('description')->nullable();
            $table->timestamps();
        });

        Schema::create('plans', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('product_id');
            $table->string('name')->nullable();
            $table->text('description')->nullable();
            $table->json('features')->nullable();
            $table->tinyInteger('billing_type')->default(getConstant('BILLING_ONETIME'));
            $table->boolean('is_default')->nullable()->default(false);
            $table->boolean('is_licensable')->default(false);
            $table->tinyInteger('license_validity')->nullable();
            $table->tinyInteger('license_validity_unit')->nullable();
            $table->integer('license_validity_duration')->nullable();
            $table->timestamps();
        });

        Schema::create('pricing_tiers', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('plan_id');
            $table->string('tier_name')->nullable();
            $table->decimal('price', 10, 2);
            $table->decimal('setup_fee', 10, 2)->default(0)->nullable();
            $table->tinyInteger('billing_type')->default(getConstant('BILLING_ONETIME'));
            $table->tinyInteger('billing_cycle')->nullable();
            $table->integer('billing_cycle_count')->default(0)->nullable();
            $table->boolean('trial_available')->default(false)->nullable();
            $table->integer('trial_duration')->nullable();
            $table->tinyInteger('trial_duration_unit')->nullable();
            $table->boolean('is_default')->default(false);
            $table->timestamps();
        });

        Schema::create('addons', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('product_id')->index();
            $table->bigInteger('plan_id')->nullable()->index(); // Nullable for product-level addons
            $table->string('name');
            $table->text('description')->nullable();
//            $table->decimal('price', 10)->nullable();

            // Billing details
            $table->tinyInteger('billing_type')->nullable(); // Defines if add-on is recurring or one-time
//            $table->integer('billing_cycle_count')->nullable()->default(0);

            // Trial details
//            $table->boolean('trial_available')->default(false);
//            $table->integer('trial_duration')->nullable(); // Trial duration (e.g., 7 days)
//            $table->enum('trial_duration_unit', getConstantValues('trial_duration_unit'))->nullable();

            $table->timestamps();
        });

        Schema::create('addon_plan', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('addon_id');
            $table->unsignedBigInteger('plan_id');
            $table->timestamps();

            $table->unique(['addon_id', 'plan_id']);
        });

        Schema::create('addon_pricing_tier', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('addon_id');
            $table->bigInteger('pricing_tier_id')->nullable()->index(); // Nullable for plan-level addons
            $table->decimal('price', 10)->nullable();
            $table->tinyInteger('billing_cycle_override')->nullable();
            $table->timestamps();
        });

        // table for Product Files
        Schema::create('product_files', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('product_id')->index();
            $table->string('version')->nullable(); // e.g., "v1.0.3"
            $table->text('changelog')->nullable();
            $table->timestamp('released_at')->nullable(); // used for update window logic
            $table->boolean('is_global')->default(false); // true = available to all plans of the product
            $table->timestamps();
        });


        Schema::create('plan_product_file', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('plan_id')->index();
            $table->bigInteger('product_file_id')->index();
            $table->enum('access_level', ['default', 'security_patch', 'always_accessible'])->default('default');
            // default = follow access window rules
            // security_patch = always available
            // always_accessible = even if access expired
            $table->timestamps();
            $table->unique(['plan_id', 'product_file_id']);
        });

        Schema::create('support_policies', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('product_id')->index();
            $table->boolean('enabled')->default(false);

            // Support Duration
            $table->integer('support_duration')->default(0); // e.g. 6
            $table->tinyInteger('support_duration_unit')->default(getConstant(
                'SUPPORT_DURATION_MONTHS'
            ));
//            $table->enum('support_start_from', ['purchase_date', 'custom'])->default('purchase_date');
            $table->text('support_description')->nullable();

            // Post-Support Pricing
            $table->tinyInteger('discount_type')->nullable();
            $table->decimal('discount_value', 10, 2)->nullable();

            // Renewal Reminders
//            $table->boolean('enable_renewal_reminders')->default(false);
//            $table->integer('reminder_days_before_expiry')->nullable();

            $table->timestamps();
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('support_policies');
        Schema::dropIfExists('plan_product_file');
        Schema::dropIfExists('product_files');
        Schema::dropIfExists('pricing_tiers');
        Schema::dropIfExists('plans');
        Schema::dropIfExists('addon_pricing_tier');
        Schema::dropIfExists('addons');
        Schema::dropIfExists('plan_product_versions');
        Schema::dropIfExists('product_versions');
        Schema::dropIfExists('products');
    }
}
