<?php

namespace App\Observers;

use App\Models\Warehouse;
use App\Services\WarehouseLogService;
use Illuminate\Support\Facades\Schema;

class WarehouseObserver
{
    /**
     * Handle the Warehouse "created" event.
     */
    public function created(Warehouse $warehouse): void
    {
        // Create log table for the new warehouse
        WarehouseLogService::createLogTable($warehouse->id);
    }

    /**
     * Handle the Warehouse "updated" event.
     */
    public function updated(Warehouse $warehouse): void
    {
        //
    }

    /**
     * Handle the Warehouse "deleted" event.
     */
    public function deleted(Warehouse $warehouse): void
    {
        // Optionally drop the log table when warehouse is deleted
        // Uncomment if you want to remove log tables when warehouses are deleted
        // $tableName = WarehouseLogService::getLogTableName($warehouse->id);
        // if (Schema::hasTable($tableName)) {
        //     Schema::dropIfExists($tableName);
        // }
    }

    /**
     * Handle the Warehouse "restored" event.
     */
    public function restored(Warehouse $warehouse): void
    {
        // Ensure log table exists when warehouse is restored
        WarehouseLogService::createLogTable($warehouse->id);
    }

    /**
     * Handle the Warehouse "force deleted" event.
     */
    public function forceDeleted(Warehouse $warehouse): void
    {
        //
    }
}
