<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\Branch\StoreBranchRequest;
use App\Http\Requests\Api\V1\Branch\UpdateBranchRequest;
use App\Http\Resources\Api\V1\BranchResource;
use App\Services\BranchService;
use App\Services\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class BranchController extends Controller
{
    protected BranchService $branchService;
    protected ResponseService $responseService;

    public function __construct(BranchService $branchService, ResponseService $responseService)
    {
        $this->branchService = $branchService;
        $this->responseService = $responseService;
    }

    public function index(Request $request): JsonResponse
    {
        try {
            $perPage = $request->input('per_page', 15);
            $branches = $this->branchService->getAllBranches($perPage);

            return $this->responseService->paginated(
                $branches,
                'Branches retrieved successfully',
                BranchResource::class
            );
        } catch (\Exception $e) {
            return $this->responseService->serverError(
                'Failed to retrieve branches',
                $e->getMessage()
            );
        }
    }

    public function store(StoreBranchRequest $request): JsonResponse
    {
        try {
            $branch = $this->branchService->createBranch($request->validated());

            return $this->responseService->created(
                new BranchResource($branch),
                'Branch created successfully'
            );
        } catch (\Exception $e) {
            return $this->responseService->serverError(
                'Failed to create branch',
                $e->getMessage()
            );
        }
    }

    public function show(int $id): JsonResponse
    {
        try {
            $branch = $this->branchService->getBranch($id);

            return $this->responseService->success(
                new BranchResource($branch),
                'Branch retrieved successfully'
            );
        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return $this->responseService->notFound('Branch not found');
        } catch (\Exception $e) {
            return $this->responseService->serverError(
                'Failed to retrieve branch',
                $e->getMessage()
            );
        }
    }

    public function update(UpdateBranchRequest $request, int $id): JsonResponse
    {
        try {
            $branch = $this->branchService->updateBranch($id, $request->validated());

            return $this->responseService->success(
                new BranchResource($branch),
                'Branch updated successfully'
            );
        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return $this->responseService->notFound('Branch not found');
        } catch (\Exception $e) {
            return $this->responseService->serverError(
                'Failed to update branch',
                $e->getMessage()
            );
        }
    }

    public function destroy(int $id): JsonResponse
    {
        try {
            $this->branchService->deleteBranch($id);

            return $this->responseService->noContent('Branch deleted successfully');
        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return $this->responseService->notFound('Branch not found');
        } catch (\Exception $e) {
            return $this->responseService->serverError(
                'Failed to delete branch',
                $e->getMessage()
            );
        }
    }

    public function updateSettings(Request $request, int $id): JsonResponse
    {
        try {
            $request->validate([
                'settings' => ['required', 'array'],
            ]);

            $branch = $this->branchService->updateBranchSettings($id, $request->input('settings'));

            return $this->responseService->success(
                new BranchResource($branch),
                'Branch settings updated successfully'
            );
        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return $this->responseService->notFound('Branch not found');
        } catch (\Illuminate\Validation\ValidationException $e) {
            return $this->responseService->validationError($e->errors());
        } catch (\Exception $e) {
            return $this->responseService->serverError(
                'Failed to update branch settings',
                $e->getMessage()
            );
        }
    }
}
