<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Drop the unique constraint by name
        DB::statement('ALTER TABLE shop_product_prices DROP INDEX shop_product_prices_shop_product_id_shop_id_unique');
        
        // Make shop_id nullable to support company products
        Schema::table('shop_product_prices', function (Blueprint $table) {
            $table->unsignedBigInteger('shop_id')->nullable()->change();
        });
        
        // Re-add foreign key constraints
        Schema::table('shop_product_prices', function (Blueprint $table) {
            $table->foreign('shop_product_id')->references('id')->on('shop_products')->onDelete('cascade');
            $table->foreign('shop_id')->references('id')->on('accounts')->onDelete('set null');
        });
        
        // Add new price columns
        Schema::table('shop_product_prices', function (Blueprint $table) {
            $table->decimal('cost_price', 15, 2)->nullable()->default(0)->after('shop_id');
            $table->decimal('selling_price', 15, 2)->nullable()->default(0)->after('cost_price');
        });
        
        // Migrate existing price data to selling_price
        DB::statement('UPDATE shop_product_prices SET selling_price = price WHERE selling_price IS NULL OR selling_price = 0');
        
        // Drop the old price column
        Schema::table('shop_product_prices', function (Blueprint $table) {
            $table->dropColumn('price');
        });
        
        // Re-add unique constraint (MySQL allows multiple NULLs in unique constraint)
        Schema::table('shop_product_prices', function (Blueprint $table) {
            $table->unique(['shop_product_id', 'shop_id'], 'shop_product_prices_unique');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Drop the unique constraint
        Schema::table('shop_product_prices', function (Blueprint $table) {
            $table->dropUnique('shop_product_prices_unique');
        });
        
        Schema::table('shop_product_prices', function (Blueprint $table) {
            $table->decimal('price', 15, 2)->default(0)->after('shop_id');
        });
        
        // Migrate selling_price back to price
        DB::statement('UPDATE shop_product_prices SET price = selling_price WHERE price IS NULL OR price = 0');
        
        Schema::table('shop_product_prices', function (Blueprint $table) {
            $table->dropColumn(['cost_price', 'selling_price']);
        });
        
        // Make shop_id not nullable again
        Schema::table('shop_product_prices', function (Blueprint $table) {
            $table->unsignedBigInteger('shop_id')->nullable(false)->change();
        });
        
        // Re-add original unique constraint
        Schema::table('shop_product_prices', function (Blueprint $table) {
            $table->unique(['shop_product_id', 'shop_id']);
        });
    }
};
