<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('employee_station', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('employee_id');
            $table->unsignedBigInteger('station_id');
            $table->timestamp('assigned_at')->nullable();
            $table->string('assigned_by')->nullable();
            $table->softDeletes(); // Adds deleted_at column
            $table->timestamps();
            
            // Foreign keys
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('station_id')->references('station_id')->on('stations')->onDelete('cascade');
            
            // Unique constraint to prevent duplicate assignments
            $table->unique(['employee_id', 'station_id'], 'unique_employee_station');
            
            // Indexes for performance
            $table->index(['station_id', 'deleted_at']);
            $table->index(['employee_id', 'deleted_at']);
        });
    }

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