<?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('attendance_sessions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->date('attendance_date');
            $table->dateTime('start_time');
            $table->dateTime('stop_time')->nullable();
            $table->string('start_video_path');
            $table->string('stop_video_path')->nullable();
            $table->unsignedInteger('start_video_seconds')->default(60);
            $table->unsignedInteger('stop_video_seconds')->nullable();
            $table->unsignedInteger('total_work_minutes')->nullable();
            $table->unsignedInteger('lunch_minutes')->default(0);
            $table->unsignedInteger('net_work_minutes')->nullable();
            $table->string('status')->default('active');
            $table->timestamps();

            $table->unique(['user_id', 'attendance_date']);
        });

        Schema::create('lunch_breaks', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('attendance_session_id')->constrained()->cascadeOnDelete();
            $table->dateTime('start_time');
            $table->dateTime('end_time')->nullable();
            $table->unsignedInteger('duration_minutes')->nullable();
            $table->boolean('is_over_limit')->default(false);
            $table->timestamps();
        });

        Schema::create('daily_work_logs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('attendance_session_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('title');
            $table->text('description');
            $table->string('completion_status')->default('in_progress');
            $table->text('blocker')->nullable();
            $table->text('remark')->nullable();
            $table->timestamps();
        });

        Schema::create('tasks', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('assigned_to')->constrained('users')->cascadeOnDelete();
            $table->foreignId('assigned_by')->constrained('users')->cascadeOnDelete();
            $table->string('title');
            $table->text('description');
            $table->date('deadline')->nullable();
            $table->string('priority')->default('medium');
            $table->decimal('point_value', 10, 2)->default(0);
            $table->text('expected_output')->nullable();
            $table->unsignedTinyInteger('completion_percentage')->default(0);
            $table->unsignedTinyInteger('admin_override_percentage')->nullable();
            $table->string('status')->default('assigned');
            $table->text('feedback')->nullable();
            $table->timestamps();
        });

        Schema::create('task_progress_logs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('task_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->unsignedTinyInteger('progress_percentage');
            $table->text('note')->nullable();
            $table->timestamps();
        });

        Schema::create('task_submissions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('task_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->text('summary')->nullable();
            $table->string('live_link')->nullable();
            $table->string('project_link')->nullable();
            $table->string('repository_link')->nullable();
            $table->string('status')->default('pending');
            $table->decimal('point_awarded', 10, 2)->nullable();
            $table->text('review_note')->nullable();
            $table->foreignId('reviewed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->dateTime('reviewed_at')->nullable();
            $table->timestamps();
        });

        Schema::create('task_submission_files', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('task_submission_id')->constrained()->cascadeOnDelete();
            $table->string('file_type');
            $table->string('file_path');
            $table->timestamps();
        });

        Schema::create('points', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('task_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('task_submission_id')->nullable()->constrained()->nullOnDelete();
            $table->decimal('points', 10, 2);
            $table->string('reason');
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->dateTime('approved_at')->nullable();
            $table->timestamps();
        });

        Schema::create('salary_rules', function (Blueprint $table): void {
            $table->id();
            $table->decimal('point_value', 10, 2)->default(1000);
            $table->decimal('monthly_target_point', 10, 2)->default(15);
            $table->boolean('is_active')->default(true);
            $table->date('effective_from')->nullable();
            $table->timestamps();
        });

        Schema::create('salary_records', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->unsignedTinyInteger('salary_month');
            $table->unsignedSmallInteger('salary_year');
            $table->decimal('earned_points', 10, 2);
            $table->decimal('target_points', 10, 2);
            $table->decimal('point_value', 10, 2);
            $table->decimal('earned_salary', 12, 2);
            $table->text('shortfall_reason')->nullable();
            $table->dateTime('generated_at');
            $table->timestamps();
        });

        Schema::create('settings', function (Blueprint $table): void {
            $table->id();
            $table->string('key')->unique();
            $table->text('value')->nullable();
            $table->text('description')->nullable();
            $table->timestamps();
        });

        Schema::create('activity_logs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('action');
            $table->text('description')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('activity_logs');
        Schema::dropIfExists('settings');
        Schema::dropIfExists('salary_records');
        Schema::dropIfExists('salary_rules');
        Schema::dropIfExists('points');
        Schema::dropIfExists('task_submission_files');
        Schema::dropIfExists('task_submissions');
        Schema::dropIfExists('task_progress_logs');
        Schema::dropIfExists('tasks');
        Schema::dropIfExists('daily_work_logs');
        Schema::dropIfExists('lunch_breaks');
        Schema::dropIfExists('attendance_sessions');
    }
};
