<?php

namespace App\Http\Controllers\Api\V1\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreCommissionDataRequest;
use App\Http\Requests\UpdateCommissionDataRequest;
use App\Http\Resources\Admin\CommissionDataResource;
use App\Models\CommissionData;
use Gate;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class CommissionDataApiController extends Controller
{
    public function index()
    {
        abort_if(Gate::denies('commission_data_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        return new CommissionDataResource(CommissionData::with(['commission', 'commission_category_name', 'commission_type', 'commission_value_fusion_types_1', 'commission_value_fusion_types_2', 'team'])->get());
    }

    public function store(StoreCommissionDataRequest $request)
    {
        $commissionData = CommissionData::create($request->all());

        return (new CommissionDataResource($commissionData))
            ->response()
            ->setStatusCode(Response::HTTP_CREATED);
    }

    public function show(CommissionData $commissionData)
    {
        abort_if(Gate::denies('commission_data_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        return new CommissionDataResource($commissionData->load(['commission', 'commission_category_name', 'commission_type', 'commission_value_fusion_types_1', 'commission_value_fusion_types_2', 'team']));
    }

    public function update(UpdateCommissionDataRequest $request, CommissionData $commissionData)
    {
        $commissionData->update($request->all());

        return (new CommissionDataResource($commissionData))
            ->response()
            ->setStatusCode(Response::HTTP_ACCEPTED);
    }

    public function destroy(CommissionData $commissionData)
    {
        abort_if(Gate::denies('commission_data_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $commissionData->delete();

        return response(null, Response::HTTP_NO_CONTENT);
    }
}
