<?php

namespace App\Http\Controllers\Api\BreederCollection;

use Illuminate\Http\Request;
use App\Services\BreederService;
use App\Http\Controllers\Controller;
use Essa\APIToolKit\Api\ApiResponse;
use App\Http\Requests\DisplayRequest;
use App\Http\Resources\BreederResource;
use App\Http\Requests\Breeder\BreederRequest;

class BreederController extends Controller
{
    use ApiResponse;

    protected BreederService $breederService;

    public function __construct(BreederService $breederService)
    {
        $this->breederService = $breederService;
    }

    public function index(DisplayRequest $request)
    {
        $status = $request->status;
        $pagination = $request->pagination;

        $breeder = $this->breederService->getAll($status);

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

        if (!$pagination) {
            BreederResource::collection($breeder);
        } else {
            $breeder = BreederResource::collection($breeder);
        }

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

    public function show($id)
    {
        $breeder = $this->breederService->getById($id);

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

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

    public function store(BreederRequest $request)
    {
        $breeder = $this->breederService->create($request->all());

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

    public function update(BreederRequest $request, $id)
    {
        $breeder = $this->breederService->update($request->all(), $id);

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

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

        return $this->responseSuccess($message, new BreederResource($breeder));
    }

    public function toggleArchive($id)
    {
        $breeder = $this->breederService->toggleArchive($id);

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

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

        return $this->responseSuccess(
            $message,
            new BreederResource($breeder["breeder"])
        );
    }
}
