<?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('employee_emergency_contacts', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->string('full_name', 255);
            $table->string('relationship', 100);
            $table->string('phone', 40);
            $table->string('phone_alt', 40)->nullable();
            $table->string('email', 255)->nullable();
            $table->string('address', 500)->nullable();
            $table->boolean('is_primary')->default(false);
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['tenant_id', 'employee_id']);
        });

        Schema::create('employee_skills', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->string('name', 255);
            $table->string('level', 32);
            $table->unsignedTinyInteger('years_experience')->nullable();
            $table->boolean('is_certified')->default(false);
            $table->string('notes', 500)->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['tenant_id', 'employee_id']);
            $table->unique(['employee_id', 'name']);
        });

        Schema::create('employee_asset_assignments', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->string('asset_code', 120);
            $table->string('asset_name', 255);
            $table->string('category', 120)->nullable();
            $table->date('assigned_at');
            $table->date('returned_at')->nullable();
            $table->string('status', 32)->default('assigned');
            $table->string('notes', 500)->nullable();
            $table->unsignedBigInteger('external_asset_id')->nullable()->index();
            $table->foreignId('assigned_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['tenant_id', 'employee_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('employee_asset_assignments');
        Schema::dropIfExists('employee_skills');
        Schema::dropIfExists('employee_emergency_contacts');
    }
};
