<?php

namespace Modules\Internal\Http\Controllers\Api;

use App\Helpers\Helper;
use App\Models\ClaimRequest;
use App\Services\ClaimService;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Internal\Transformers\ClaimRequestResource;
use Modules\Internal\Transformers\ClaimRequestShowResource;

class ClaimRequestController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index(Request $request)
    {
        $claimRequests = ClaimRequest::query()
                                ->when($request->search, function ($q, $search) {
                                    $q->where('code', 'LIKE', "%".$search."%");
                                })
                                ->when($request->orderBy, function ($q, $orderBy) use ($request) {
                                    if (in_array($orderBy, ['submission_date', 'code'])) {
                                        $q->orderBy($orderBy, $request->order);
                                    }
                                })
                                ->when(empty($request->orderBy), function ($q) {
                                    $q->orderBy('created_at', 'desc');
                                })
                                ->when($request->status, function($q, $status) {
                                    $q->where('status', $status);
                                })
                                ->with(['member', 'files', 'service'])
                                ->paginate();

        return Helper::paginateResources(ClaimRequestResource::collection($claimRequests));
    }

    /**
     * Show the form for creating a new resource.
     * @return Renderable
     */
    public function create()
    {
        return view('internal::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param Request $request
     * @return Renderable
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {
        $claimRequest = ClaimRequest::findOrFail($id);
        $claimRequest->load([
            'histories' => function ($history) {
                $history->latest();
            },
            'files'
        ]);

        return Helper::responseJson(data: ClaimRequestShowResource::make($claimRequest));
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit($id)
    {
        return view('internal::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy($id)
    {
        //
    }

    public function approve($id)
    {
        $claimRequest = ClaimRequest::findOrFail($id);
        $member = $claimRequest->member;
        
        try {

            // Create New Claim
            $newClaim = ClaimService::storeClaim(member: $member, status: 'received', claimRequest: $claimRequest);
            
            // Update Claim Request Status & Link with Claim
            $claimRequest->status = 'approved';
            $claimRequest->claim_id = $newClaim->id;
            $claimRequest->save();
            
            // Store Generated Documents LOG
            $logContent = view('pdf.guaranted_leter', compact('member', 'claimRequest'));
            $claimRequest->generatedDocuments()->create([
                'type' => 'guarantee_letter',
                'title' => 'Guarantee Letter for '. $member->full_name,
                'document_type' => 'type',
                'html_content' => $logContent,
                'system_origin' => 'primecenter'
            ]);

        } catch (\Exception $e) {

        }

        return $claimRequest;
    }

}
