<?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('weekoff_rules', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->nullable()->index();
            $table->string('name', 191);
            $table->text('description')->nullable();
            $table->date('effective_from');
            $table->string('status', 20)->default('active');
            $table->boolean('half_day_enabled')->default(false);
            $table->boolean('is_company_default')->default(false);
            $table->json('schedule');
            $table->timestamps();
        });

        Schema::create('weekoff_rule_assignments', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->nullable()->index();
            $table->unsignedBigInteger('weekoff_rule_id');
            $table->unsignedBigInteger('user_id');
            $table->date('effective_from');
            $table->unsignedBigInteger('assigned_by')->nullable();
            $table->timestamps();

            $table->index(['user_id', 'effective_from']);
            $table->index(['weekoff_rule_id', 'user_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('weekoff_rule_assignments');
        Schema::dropIfExists('weekoff_rules');
    }
};
