<?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
    {
        Schema::table('sales_products', function (Blueprint $table) {
            $table->decimal('partner_commission_rate', 5, 2)->default(20.00)->after('subscription_fee')->comment('영업파트너 수당율(%)');
            $table->decimal('manager_commission_rate', 5, 2)->default(5.00)->after('partner_commission_rate')->comment('매니저 수당율(%)');
        });

        // 기존 데이터 업데이트: commission_rate 25% → partner 20%, manager 5%
        DB::table('sales_products')->update([
            'partner_commission_rate' => 20.00,
            'manager_commission_rate' => 5.00,
        ]);

        Schema::table('sales_products', function (Blueprint $table) {
            $table->dropColumn('commission_rate');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('sales_products', function (Blueprint $table) {
            $table->decimal('commission_rate', 5, 2)->default(25.00)->after('subscription_fee')->comment('수당율(%)');
        });

        // 롤백 시 partner + manager 합산으로 복원
        DB::statement('UPDATE sales_products SET commission_rate = partner_commission_rate + manager_commission_rate');

        Schema::table('sales_products', function (Blueprint $table) {
            $table->dropColumn(['partner_commission_rate', 'manager_commission_rate']);
        });
    }
};
