<?php

namespace App\Http\Controllers\Api\Admin;

use App\Actions\Setting\UpdateSettingsAction;
use App\Enums\SettingGroup;
use App\Http\Controllers\Controller;
use App\Http\Requests\Setting\UpdateSettingsRequest;
use App\Http\Resources\SettingResource;
use App\Repositories\Contracts\SettingRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use OpenApi\Attributes as OA;

class SettingController extends Controller
{
    public function __construct(private SettingRepositoryInterface $repository) {}

    #[OA\Get(
        path: '/api/admin/settings',
        summary: 'Tüm ayarları listele (Admin)',
        tags: ['Admin - Settings'],
        security: [['sanctum' => []]],
        responses: [new OA\Response(response: 200, description: 'Ayar listesi')],
    )]
    public function index(): AnonymousResourceCollection
    {
        return SettingResource::collection($this->repository->all());
    }

    #[OA\Get(
        path: '/api/admin/settings/group/{group}',
        summary: 'Gruba göre ayarları getir',
        tags: ['Admin - Settings'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'group', in: 'path', required: true, schema: new OA\Schema(type: 'string'))],
        responses: [
            new OA\Response(response: 200, description: 'Grup ayarları'),
            new OA\Response(response: 404, description: 'Grup bulunamadı'),
        ],
    )]
    public function group(string $group): AnonymousResourceCollection
    {
        $settingGroup = SettingGroup::tryFrom($group);

        if (! $settingGroup) {
            abort(404, 'Ayar grubu bulunamadı.');
        }

        return SettingResource::collection($this->repository->getByGroup($settingGroup));
    }

    #[OA\Put(
        path: '/api/admin/settings',
        summary: 'Ayarları toplu güncelle (dot notation: general.site_name)',
        tags: ['Admin - Settings'],
        security: [['sanctum' => []]],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            required: ['settings'],
            properties: [
                new OA\Property(property: 'settings', type: 'object', example: '{"general.site_name": "Yeni Ad", "contact.phone_primary": "+90 ..."}'),
            ],
        )),
        responses: [
            new OA\Response(response: 200, description: 'Ayarlar güncellendi'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function update(UpdateSettingsRequest $request, UpdateSettingsAction $action): JsonResponse
    {
        $action->execute($request->validated('settings'));

        return response()->json(['message' => 'Ayarlar güncellendi.']);
    }

    #[OA\Post(
        path: '/api/admin/settings/clear-cache',
        summary: 'Ayar cache temizle',
        tags: ['Admin - Settings'],
        security: [['sanctum' => []]],
        responses: [new OA\Response(response: 200, description: 'Cache temizlendi')],
    )]
    public function clearCache(): JsonResponse
    {
        $this->repository->clearCache();

        return response()->json(['message' => 'Ayar cache temizlendi.']);
    }
}
