<?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
    {
        // Partner leave requests (when partner wants a day off)
        Schema::create('partner_leaves', function (Blueprint $table) {
            $table->id();
            $table->foreignId('partner_id')->constrained('partner_profiles')->cascadeOnDelete();
            $table->foreignId('booking_id')->nullable()->constrained('bookings')->nullOnDelete(); // affected sub
            $table->date('leave_date');
            $table->date('leave_end_date')->nullable(); // for multi-day leaves
            $table->enum('leave_type', ['full_day', 'half_day', 'multi_day'])->default('full_day');
            $table->string('reason', 240)->nullable();
            $table->enum('status', ['pending', 'approved', 'rejected', 'cancelled'])->default('pending');
            $table->string('replacement_partner_id', 32)->nullable(); // who'll cover
            $table->text('admin_notes')->nullable();
            $table->timestamp('responded_at')->nullable();
            $table->timestamps();

            $table->index(['partner_id', 'leave_date']);
            $table->index(['leave_date', 'status']);
        });

        // Partner daily attendance with optional screenshot proof
        Schema::create('partner_attendances', function (Blueprint $table) {
            $table->id();
            $table->foreignId('partner_id')->constrained('partner_profiles')->cascadeOnDelete();
            $table->date('attendance_date');
            $table->enum('attendance_type', ['check_in', 'check_out'])->default('check_in');
            $table->timestamp('logged_at');
            $table->decimal('lat', 10, 7)->nullable();
            $table->decimal('lng', 10, 7)->nullable();
            $table->string('screenshot_path')->nullable(); // storage path
            $table->string('note', 240)->nullable();
            $table->timestamps();

            $table->index(['partner_id', 'attendance_date']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('partner_attendances');
        Schema::dropIfExists('partner_leaves');
    }
};
