<?php

declare(strict_types=1);

namespace App\Http\Controllers\Property;

use App\Http\Controllers\AbstractController;
use App\Models\Property\Property;
use App\Models\Property\Referral;
use App\Http\Controllers\Helpers\Permissions\UserPermissions;
use App\Enums\UserActions;
use App\Http\Controllers\Helpers\ResponseHelper;
use Hypervel\Http\Request;
use Hypervel\Support\Facades\Auth;

class PropertyManagementController extends AbstractController
{
    public function listProperties(Request $request)
    {
        if (!UserPermissions::isActionPermitted(Auth::user()->acct_type, UserActions::ViewProperties)) {
            return ResponseHelper::returnUnauthorized();
        }

        $properties = Property::with(['creator'])->where('is_active', true)->get();
        return response()->json([
            'properties' => $properties,
        ]);
    }

    public function listReferrals(Request $request)
    {
        if (!UserPermissions::isActionPermitted(Auth::user()->acct_type, UserActions::ViewReferrals)) {
            return ResponseHelper::returnUnauthorized();
        }

        $referrals = Referral::with(['property', 'referrer', 'referred', 'creator'])->where('is_active', true)->get();
        return response()->json([
            'referrals' => $referrals,
        ]);
    }
}
