<?php

namespace App\Http\Controllers\v1\Tenancy\Visit;

use App\Http\Controllers\Controller;
use App\Models\Tenancy\Procedure;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;

class GetProcedureController extends Controller
{
    public function __invoke(int $id): Response
    {
        $procedure = Procedure::findOrFail($id);
        if ($user = Auth::guard('sanctum')->user()) {
            if ($activeLoyalty = $user->detail->activeLoyaltyStatus()) {
                $discount = $activeLoyalty->discounts()
                    ->active()
                    ->procedure()
                    ->where('procedure_id', $id)
                    ->first();
                if ($discount) {
                    $procedure->sale = $discount->discount;
                    $procedure->status_discount = true;
                }
            }
        }

        return $this->ok($procedure);
    }
}
