<?php

namespace App\Services;

use App\Models\Building;
use App\Services\AuditTrailService;

class BuildingService
{
    protected AuditTrailService $auditTrailService;

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

    public function getAll($status)
    {
        $building = Building::with("farm")
            ->when($status === "inactive", function ($query) {
                $query->onlyTrashed();
            })
            ->useFilters()
            ->dynamicPaginate();

        return $building;
    }

    public function getById($id)
    {
        $building = Building::withTrashed()->find($id);

        return $building;
    }

    public function create(array $data)
    {
        $building = Building::create([
            "name" => $data["name"],
            "farm_id" => $data["farm_id"],
        ]);

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

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

        return $building;
    }

    public function getByFarm() {}

    public function update($id, array $data)
    {
        $building = Building::find($id);

        if (!$building) {
            return null;
        }

        $oldData = $building->getAttributes();

        $building->fill([
            "name" => $data["name"],
            "farm_id" => $data["farm_id"],
        ]);

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

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

        return $building;
    }

    public function toggleArchive($id)
    {
        $building = Building::withTrashed()->find($id);

        if (!$building) {
            return null;
        }

        $oldData = $building->getAttributes();

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

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

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

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

    public function fetchInactiveBuildings()
    {
        $user = auth()->user();

        return Building::query()
            ->with("farm")
            ->where("farm_id", $user->farm_id)
            ->whereDoesntHave("activeBatch")
            ->get();
    }

    public function fetchActiveBuilding()
    {
        $user = auth()->user();

        return Building::query()
            ->where("farm_id", $user->farm_id)
            ->whereHas("activeBatch", function ($query) {
                $query->whereHas("loading", function ($query) {
                    $query->where("status", "saved");
                });
            })
            ->with([
                "activeBatch.loading" => function ($query) {
                    $query->where("status", "saved");
                },
                "farm.leadman"
            ])
            ->get();
    }
}
