<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\CreateFranchiseRequest;
use App\Http\Requests\Admin\UpdateFranchiseBankDetailsRequest;
use App\Http\Requests\Admin\UpdateFranchiseRequest;
use App\Models\Franchise;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;

class FranchiseController extends Controller
{
    public function index(): AnonymousResourceCollection
    {
        return JsonResource::collection(
            Franchise::orderBy('name')->get([
                'id', 'name', 'city', 'status', 'commission_percentage', 'bank_account_number',
            ])
        );
    }

    public function store(CreateFranchiseRequest $request): JsonResponse
    {
        $franchise = Franchise::create([
            ...$request->validated(),
            'slug' => $this->uniqueSlug($request->validated('name')),
            'status' => 'active',
        ]);

        return response()->json(['franchise' => $franchise], 201);
    }

    public function show(Franchise $franchise): JsonResponse
    {
        return response()->json(['franchise' => $franchise]);
    }

    public function update(UpdateFranchiseRequest $request, Franchise $franchise): JsonResponse
    {
        $franchise->update($request->validated());

        return response()->json(['franchise' => $franchise->fresh()]);
    }

    public function updateBankDetails(UpdateFranchiseBankDetailsRequest $request, Franchise $franchise): JsonResponse
    {
        $franchise->update($request->validated());

        return response()->json(['franchise' => $franchise->fresh()]);
    }

    /** Franchise names can collide (two "MedPlus" locations in different cities) - the slug still has to be unique. */
    private function uniqueSlug(string $name): string
    {
        $base = Str::slug($name);
        $slug = $base;
        $suffix = 1;

        while (Franchise::where('slug', $slug)->exists()) {
            $slug = "{$base}-".++$suffix;
        }

        return $slug;
    }
}
