<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('marketing_officer_payments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('marketing_officer_id')->constrained('users')->onDelete('cascade');
            $table->date('period_start'); // Monday of the 2-week period
            $table->date('period_end');   // Sunday of the 2-week period
            $table->decimal('commission_amount', 10, 2)->default(0);
            $table->enum('status', ['pending', 'paid'])->default('pending');
            $table->timestamp('paid_at')->nullable();
            $table->foreignId('paid_by')->nullable()->constrained('users')->onDelete('set null');
            $table->text('notes')->nullable();
            $table->timestamps();
            
            // Index for faster queries with shorter name
            $table->index(['marketing_officer_id', 'period_start'], 'mo_payments_officer_period_idx');
        });
    }

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