<?php

namespace App\Http\Controllers\Api\Company;

use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\Report;
use Illuminate\Http\Request;

class CompanyReportController extends Controller
{
    public function index(Request $request)
    {
        $user = $request->user();

        // Alan adı application_status'tur ("status" diye kolon yok — hep 403 dönerdi).
        abort_unless(
            $user->company && $user->company->application_status === Company::STATUS_APPROVED,
            403,
            'Herhangi bir şirketiniz bulunmamakta veya şirketiniz henüz onaylanmamış.'
        );

        $companyId = $user->company->id;

        $reports = Report::query()
            ->with([
                'user:id,name,email',
                'listing:id,slug,title,city,country,status,deleted_at,consultant_profile_id',
                'listing.consultantProfile:id,company_name,slug,user_id',
                'listing.consultantProfile.user:id,name',
            ])
            ->whereHas(
                'listing.consultantProfile',
                fn ($query) => $query->where('company_id', $companyId)
            )
            ->latest()
            ->paginate(20)
            ->withQueryString();

        return response()->json($reports);
    }
}
