<?php

namespace App\Http\Controllers\Api\BuildingCollection;

use App\Http\Controllers\Controller;
use App\Http\Requests\Building\BuildingRequest;
use App\Http\Requests\Building\GetFarmByBuildingRequest;
use App\Http\Requests\DisplayRequest;
use App\Http\Resources\BuildingResource;
use App\Models\Building;
use App\Services\BuildingService;
use Essa\APIToolKit\Api\ApiResponse;
use Illuminate\Http\Request;

class BuildingController extends Controller
{
    use ApiResponse;

    protected BuildingService $buildingService;

    public function __construct(BuildingService $buildingService)
    {
        $this->buildingService = $buildingService;
    }

    public function index(DisplayRequest $request)
    {
        $status = $request->status;
        $pagination = $request->pagination;
        $buildings = $this->buildingService->getAll($status);

        if ($buildings->isEmpty()) {
            return $this->responseNotFound(
                __("messages.not_found", [
                    "module" => __("models.building_plural"),
                ])
            );
        }

        if (!$pagination) {
            BuildingResource::collection($buildings);
        } else {
            $buildings = BuildingResource::collection($buildings);
        }

        return $this->responseSuccess(
            __("messages.display", [
                "module" => __("models.building_plural"),
            ]),
            $buildings
        );
    }

    public function show($id)
    {
        $building = $this->buildingService->getById($id);

        if (!$building) {
            return $this->responseNotFound(
                __("messages.not_found", [
                    "module" => __("models.building"),
                ])
            );
        }

        return $this->responseSuccess(
            __("messages.display", [
                "module" => __("models.building"),
            ]),
            new BuildingResource($building)
        );
    }

    public function getByFarmBuilding(GetFarmByBuildingRequest $request, $id)
    {
        $filtered_building = Building::where("farm_id", $id)->dynamicPaginate();

        if ($filtered_building->isEmpty()) {
            return $this->responseNotFound(
                __("messages.no_data", [
                    "module" => __("models.building_plural"),
                ])
            );
        }

        return $this->responseSuccess(
            __("messages.display", [
                "module" => __("models.building_plural"),
            ]),
            $filtered_building
        );
    }

    public function store(BuildingRequest $request)
    {
        $building = $this->buildingService->create($request->all());

        return $this->responseCreated(
            __("messages.created", [
                "module" => __("models.building"),
            ]),
            new BuildingResource($building)
        );
    }

    public function update(BuildingRequest $request, $id)
    {
        $building = $this->buildingService->update($id, $request->all());

        if (!$building) {
            return $this->responseNotFound(
                __("messages.not_found", [
                    "module" => __("models.building"),
                ])
            );
        }

        $message = $building->was_changed
            ? __("messages.updated", ["module" => __("models.building")])
            : __("messages.no_changes", ["module" => __("models.building")]);

        return $this->responseSuccess(
            $message,
            new BuildingResource($building)
        );
    }

    public function toggleArchive($id)
    {
        $result = $this->buildingService->toggleArchive($id);

        if (!$result) {
            return $this->responseNotFound(
                __("messages.not_found", [
                    "module" => __("models.building"),
                ])
            );
        }

        $message =
            $result["status"] === "restored"
            ? __("messages.restored", ["module" => __("models.building")])
            : __("messages.archived", ["module" => __("models.building")]);

        return $this->responseSuccess(
            $message,
            new BuildingResource($result["building"])
        );
    }

    public function inactiveBatchBuildings()
    {
        $building = $this->buildingService->fetchInactiveBuildings();

        return $building;
    }

    public function fetchActiveBuilding()
    {
        $building = $this->buildingService->fetchActiveBuilding();

        return $building;
    }
}
