<?php

namespace App\Services;

use App\Models\Category;

class CategoryService
{
    protected AuditTrailService $auditTrailService;

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

    public function getAll()
    {
        $category = Category::useFilters()->dynamicPaginate();

        return $category;
    }

    public function getById(int $id)
    {
        $category = Category::withTrashed()->find($id);

        return $category;
    }

    public function create(array $data)
    {
        $category = Category::create([
            "code" => $data["code"],
            "name" => $data["name"],
        ]);

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

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

        return $category;
    }

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

        if (!$category) {
            return null;
        }

        $oldData = $category->getAttributes();

        $category->fill([
            "code" => $data["code"],
            "name" => $data["name"],
        ]);

        if ($category->isDirty()) {
            $category->save();
            $category->was_changed = true;

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

        return $category;
    }

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

        if (!$category) {
            return null;
        }

        $oldData = $category->getAttributes();

        if (!$category->trashed()) {
            $isUsed = $category
                ->items()
                ->active()
                ->exists();

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

            $category->delete();
            $message = __("models.lc_archived");
        } else {
            $category->restore();
            $message = __("models.lc_restored");
        }

        $module_type = __("models.lc_masterlist");
        $module_name = __("models.category");
        $action = $message;

        $this->auditTrailService->log(
            $module_type,
            $module_name,
            $category->id,
            $action,
            $category,
            $oldData
        );

        return [
            "category" => $category,
            "status" => $message,
        ];
    }
}
