<?php

declare(strict_types=1);

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('hour_timesheet_lines', function (Blueprint $table) {
            $table->id();
            $table->foreignId('hour_timesheet_week_id')
                ->constrained('hour_timesheet_weeks')
                ->cascadeOnDelete();
            $table->string('registration_type', 16);
            $table->foreignId('internal_category_id')
                ->nullable()
                ->constrained('hour_internal_categories')
                ->nullOnDelete();
            $table->foreignId('customer_id')
                ->nullable()
                ->constrained('customers')
                ->nullOnDelete();
            $table->foreignId('hour_project_id')
                ->nullable()
                ->constrained('hour_projects')
                ->nullOnDelete();
            $table->foreignId('hour_task_id')
                ->nullable()
                ->constrained('hour_tasks')
                ->nullOnDelete();

            foreach (['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] as $day) {
                $table->decimal('duration_'.$day, 8, 2)->default('0');
            }

            $table->string('line_approval_status', 16)->default('pending');
            $table->foreignId('approved_by_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            $table->timestamp('approved_at')->nullable();
            $table->text('reject_reason')->nullable();

            $table->timestamps();

            $table->index(
                ['registration_type', 'line_approval_status'],
                'ht_lines_reg_type_line_appr_stat_idx',
            );
        });
    }

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