<?php

namespace Modules\Patient\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Modules\Core\Helpers\ApiResponse;
use Modules\Core\Http\Requests\UpdateStatusRequest;
use Modules\Patient\Transformers\ZakatFundResource;
use Modules\Patient\DTO\PatientSettingFilter;
use Modules\Patient\Http\Requests\ReorderZakatFundRequest;
use Modules\Patient\Services\ZakatFundService;
use Modules\Patient\Models\PatientSetting;
use Modules\Patient\Http\Requests\ZakatFundRequest;
use Modules\Patient\Jobs\SyncZakatToPms;
use Modules\Patient\Models\ZakatFund;

class ZakatFundController extends Controller
{
    use AuthorizesRequests;

    protected $zakatFundService;

    public function __construct(ZakatFundService $zakatFundService)
    {
        $this->zakatFundService = $zakatFundService;
    }

    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $this->authorize('patientSettingView', PatientSetting::class);

        $filters = PatientSettingFilter::fromRequest($request);
      return  $this->zakatFundService->list($filters);
        return (ZakatFundResource::collection($this->zakatFundService->list($filters)))->additional([
            'status' => true,
            'message' => 'Zakat funds retrieved successfully.'
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(ZakatFundRequest $request)
    {
        $this->authorize('patientSettingManage', PatientSetting::class);

        return (new ZakatFundResource($this->zakatFundService->store($request->validated())))->additional([
            'status' => true,
            'message' => 'Zakat fund created successfully.'
        ]);
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        $this->authorize('patientSettingView', PatientSetting::class);

        $data = $this->zakatFundService->show($id);
        return (new ZakatFundResource($data))->additional([
            'status' => true,
            'message' => 'Zakat fund retrieved successfully.'
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(ZakatFundRequest $request, $id)
    {
        $this->authorize('patientSettingManage', PatientSetting::class);

        $zakatFund = $this->zakatFundService->update(
            $this->zakatFundService->show($id),
            $request->validated()
        );
        return (new ZakatFundResource($zakatFund))->additional([
            'status' => true,
            'message' => 'Zakat fund updated successfully.'
        ]);
    }

    public function updateStatus(UpdateStatusRequest $request, ZakatFund $zakatFund)
    {

        $this->authorize('patientSettingManage',  PatientSetting::class);

        $this->zakatFundService->updateStatus(
            $zakatFund,
            $request->validated()
        );
        return ApiResponse::successResponse('Zakat fund status updated successfully.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {

        return response()->json([]);
    }

    public function reorder(ReorderZakatFundRequest $request)
    {
        $this->authorize('patientSettingManage', PatientSetting::class);

        $this->zakatFundService->reorder($request->validated()['ids']);

        return ApiResponse::successResponse('Zakat fund order updated successfully.');
    }


    public function syncAllPending()
    {
        $pendingFunds = ZakatFund::where('is_synced', false)->get();
        if ($pendingFunds->isEmpty()) {
            return ApiResponse::errorResponse('All data is already synced.');
        }
        foreach ($pendingFunds as $fund) {
            SyncZakatToPms::dispatch($fund, 'sync');
        }
        return ApiResponse::successResponse('Sync jobs for pending zakat funds have been dispatched.');
    }
}
