<?php

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

return new class extends Migration
{
    /**
     * 영업수수료 상세 테이블 (상품별 수당 내역)
     */
    public function up(): void
    {
        Schema::create('sales_commission_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('commission_id')->comment('수수료 정산 ID');
            $table->unsignedBigInteger('contract_product_id')->comment('계약 상품 ID (sales_contract_products)');

            // 상품별 수당 계산
            $table->decimal('registration_fee', 14, 2)->comment('상품 가입비');
            $table->decimal('base_amount', 14, 2)->comment('수당 계산 기준액 (가입비의 50%)');
            $table->decimal('partner_rate', 5, 2)->comment('영업파트너 수당률 (%)');
            $table->decimal('manager_rate', 5, 2)->comment('매니저 수당률 (%)');
            $table->decimal('partner_commission', 14, 2)->comment('영업파트너 수당액');
            $table->decimal('manager_commission', 14, 2)->comment('매니저 수당액');

            $table->timestamps();

            // 인덱스 및 외래키
            $table->index('commission_id');
            $table->index('contract_product_id');

            $table->foreign('commission_id')
                ->references('id')
                ->on('sales_commissions')
                ->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('sales_commission_details');
    }
};
