<?php

namespace App\Services;

use App\Models\Warehouse;

class WarehouseService
{
    protected AuditTrailService $auditTrailService;

    public function __construct(AuditTrailService $auditTrailService)
    {
        $this->auditTrailService = $auditTrailService;
    }

    public function getAll($status)
    {
        $warehouse = Warehouse::when($status == "inactive", function ($query) {
            $query->onlyTrashed();
        })
            ->useFilters()
            ->dynamicPaginate();

        if ($warehouse->isEmpty()) {
            return null;
        }

        return $warehouse;
    }

    public function getById(int $id)
    {
        $warehouse = Warehouse::find($id);

        if (!$warehouse) {
            return null;
        }

        return $warehouse;
    }

    public function create(array $data)
    {
        $warehouse = Warehouse::create([
            "code" => $data["code"],
            "name" => $data["name"],
            "url" => $data["url"] ?? null,
            "token" => $data["token"] ?? null,
        ]);

        $module_type = __("models.lc_masterlist");
        $module_name = __("models.warehouse");
        $action = __("models.lc_created");

        $this->auditTrailService->log(
            $module_type,
            $module_name,
            $warehouse->id,
            $action,
            $warehouse,
            null,
            $data
        );

        return $warehouse;
    }

    public function update(array $data, int $id)
    {
        $warehouse = Warehouse::find($id);

        if (!$warehouse) {
            return null;
        }

        $oldData = $warehouse->getAttributes();

        if ($warehouse->isDirty()) {
            $warehouse->was_changed = true;
            $warehouse->update([
                "code" => $data["code"],
                "name" => $data["name"],
                "url" => $data["url"] ?? null,
                "token" => $data["token"] ?? null,
            ]);

            $this->auditTrailService->log(
                __("models.lc_masterlist"),
                __("models.warehouse"),
                $warehouse->id,
                __("models.lc_updated"),
                $warehouse,
                $oldData,
                $data
            );
        } else {
            $warehouse->was_changed = false;
        }

        return $warehouse;
    }

    public function toggleArchive(int $id)
    {
        $warehouse = Warehouse::withTrashed()->find($id);

        if (!$warehouse) {
            return null;
        }

        $oldData = $warehouse->getAttributes();
        $action = null;

        if (!$warehouse->trashed()) {
            $isUsed = $warehouse
                ->users()
                ->active()
                ->exists();

            if ($isUsed) {
                return [
                    "status" => "blocked",
                    "reason" => "in_use",
                    "warehouse" => $warehouse,
                ];
            }

            $warehouse->delete();
            $action = __("models.lc_archived");
            $status = "archived";
        } else {
            $warehouse->restore();
            $action = __("models.lc_restored");
            $status = "restored";
        }

        $this->auditTrailService->log(
            __("models.lc_masterlist"),
            __("models.warehouse"),
            $warehouse->id,
            $action,
            $warehouse,
            $oldData,
            null
        );

        return [
            "status" => $status,
            "warehouse" => $warehouse,
        ];
    }
}
