<?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('employees', function (Blueprint $table) {
            $table->id();
            $table->string('employee_code', 50)->unique()->comment('Contoh: EMP-2026-001');
            $table->string('name', 255);
            
            // REVISI: Menggunakan relasi ke Master Jabatan
            $table->bigInteger('position_id')->unsigned();
            
            $table->bigInteger('branch_id')->unsigned()->comment('Lokasi Penugasan / Cost Center Cabang');
            $table->bigInteger('user_id')->unsigned()->nullable()->comment('Kosong jika tidak punya akses aplikasi');
            
            $table->string('phone', 20)->nullable();
            $table->text('address')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            // Foreign Keys
            $table->foreign('position_id')->references('id')->on('positions')->onDelete('restrict');
            $table->foreign('branch_id')->references('id')->on('branches')->onDelete('restrict');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
        });
    }

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