<?php

namespace App\Http\Controllers;

use App\Models\Breed;
use Illuminate\Http\Request;;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\DB;
use App\Http\Resources\BreedResource;
use App\Support\ApiResponse;
class BreedController extends Controller
{
    public function index()
    {
        Gate::authorize('viewAny', Breed::class);
        return BreedResource::collection(Breed::all());
    }

    public function store(Request $request)
    {
        Gate::authorize('create', Breed::class);
        try {
            DB::beginTransaction();
            DB::commit();
        } catch (\Exception $exception) {
            return ApiResponse::response(message: $exception->getMessage(), status: 419);
        }
        return new BreedResource($breed);
    }

    public function show(Breed $breed)
    {
        Gate::authorize('view', $breed);
        return new BreedResource($breed);
    }

    public function update(Request $request, Breed $breed)
    {
        Gate::authorize('update', $breed);
        try {
            DB::beginTransaction();
            DB::commit();
        } catch (\Exception $exception) {
            return ApiResponse::response(message: $exception->getMessage(), status: 419);
        }
        return new BreedResource($breed);
    }

    public function destroy(Breed $breed)
    {
        Gate::authorize('delete', $breed);
        try {
            DB::beginTransaction();
            DB::commit();
        } catch (\Exception $exception) {
            return ApiResponse::response(message: $exception->getMessage(), status: 419);
        }
        return ApiResponse::success(message: 'با موفقیت حذف شد');
    }
}
