<?php

namespace App\Http\Controllers\API\Investor;

use App\Http\Controllers\API\BaseApiController;
use App\Models\InvestmentOpportunity;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class InvestorOpportunityController extends BaseApiController
{
    public function index(Request $request): JsonResponse
    {
        $opportunities = InvestmentOpportunity::with(['province', 'district'])
            ->withCount('expressionsOfInterest')
            ->where('status', 'pronto')
            ->when($request->sector, fn($q) => $q->where('sector', $request->sector))
            ->when($request->province_id, fn($q) => $q->where('province_id', $request->province_id))
            ->when($request->search, fn($q) =>
                $q->where('title_pt', 'like', "%{$request->search}%"))
            ->orderByDesc('is_featured')
            ->orderByDesc('published_at')
            ->paginate(12);

        return $this->paginatedResponse($opportunities);
    }

    public function show(string $code): JsonResponse
    {
        $opportunity = InvestmentOpportunity::with(['province', 'district'])
            ->withCount('expressionsOfInterest')
            ->where('code', $code)
            ->where('status', 'pronto')
            ->firstOrFail();

        return $this->successResponse($opportunity);
    }
}
