<?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('locker_categories', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->string('code', 10)->unique();
            $table->decimal('base_price', 10, 2)->default(0);
            $table->text('description')->nullable();
            $table->integer('display_order')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('locker_zones', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->string('code', 20)->unique();
            $table->string('floor')->nullable();
            $table->enum('gender_restriction', ['none', 'male', 'female'])->default('none');
            $table->text('description')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('lockers', function (Blueprint $table) {
            $table->id();
            $table->string('locker_number', 50)->unique();
            $table->foreignId('category_id')->constrained('locker_categories')->restrictOnDelete();
            $table->foreignId('zone_id')->nullable()->constrained('locker_zones')->nullOnDelete();
            $table->enum('status', ['available', 'occupied', 'maintenance', 'reserved'])->default('available');
            $table->string('physical_key_id', 100)->nullable()->unique();
            $table->decimal('deposit_amount', 10, 2)->default(0);
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index('status');
            $table->index('category_id');
            $table->index('zone_id');
        });

        // Locker pricing by duration
        Schema::create('locker_pricing', function (Blueprint $table) {
            $table->id();
            $table->foreignId('category_id')->constrained('locker_categories')->cascadeOnDelete();
            $table->string('duration_label', 50); // "1 Month", "3 Months", etc.
            $table->integer('duration_days');
            $table->decimal('price', 10, 2);
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->index(['category_id', 'is_active']);
        });

        // Maintenance log for lockers
        Schema::create('locker_maintenance_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('locker_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->restrictOnDelete();
            $table->string('reason');
            $table->enum('action', ['set_maintenance', 'resolved']);
            $table->text('notes')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('locker_maintenance_logs');
        Schema::dropIfExists('locker_pricing');
        Schema::dropIfExists('lockers');
        Schema::dropIfExists('locker_zones');
        Schema::dropIfExists('locker_categories');
    }
};
