<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use App\Models\LeasingProfile;
use App\Models\Branch;
use App\Services\RecordSearchService;
use Illuminate\Http\Request;

class SearchController extends Controller
{
    protected $searchService;
    protected $formatterService;

    public function __construct(RecordSearchService $searchService, \App\Services\MemberRecordFormatterService $formatterService)
    {
        $this->searchService = $searchService;
        $this->formatterService = $formatterService;
    }

    public function index(Request $request)
    {
        $filters = $request->only(['query', 'leasing_id', 'branch_id']);
        $results = $this->searchService->search($filters);
        
        $leasings = LeasingProfile::all();
        $branches = collect();
        if ($request->filled('leasing_id')) {
            $branches = Branch::where('leasing_id', $request->leasing_id)->get();
        }

        return view('member.search.index', compact('results', 'leasings', 'branches', 'filters'));
    }

    public function show($id)
    {
        $record = $this->searchService->findById($id);
        $formatted = $this->formatterService->format($record);
        
        return view('member.search.show', compact('record', 'formatted'));
    }
}
