<?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::table('partner_profiles', function (Blueprint $table) {
            // Per-partner override for platform commission (in percent, 0-100).
            // If NULL, fall back to the global setting (commission.platform_percent).
            $table->decimal('commission_percent', 5, 2)->nullable()->after('suspend_reason');
            // Optional human-readable commission notes shown to the partner
            $table->text('commission_notes')->nullable()->after('commission_percent');
            // Paise accumulated as available-for-payout (post-TDS partner net)
            $table->unsignedBigInteger('wallet_balance_paise')->default(0)->after('commission_notes');
            // Paise pending in holds (visits not yet completed)
            $table->unsignedBigInteger('pending_balance_paise')->default(0)->after('wallet_balance_paise');
        });

        // Seed default commission settings
        $defaults = [
            'commission.platform_percent' => ['15', 'int'],
            'commission.first_month_bonus_paise' => ['0', 'int'],
            'commission.tds_percent' => ['0', 'int'],
            'commission.min_payout_paise' => ['10000', 'int'],
            'commission.policy_note' => ['Platform commission is deducted from each completed visit and active subscription. TDS may apply as per prevailing tax rules.', 'text'],
        ];
        foreach ($defaults as $key => [$value, $type]) {
            \App\Models\Setting::set($key, $value, $type, 'commission');
        }
    }

    public function down(): void
    {
        Schema::table('partner_profiles', function (Blueprint $table) {
            $table->dropColumn(['commission_percent', 'commission_notes', 'wallet_balance_paise', 'pending_balance_paise']);
        });
    }
};