<?php
namespace App\Http\Controllers;

use App\Models\Scholarship;
use App\Models\Country;
use App\Models\University;
use Illuminate\Http\Request;

class ScholarshipController extends Controller
{
    public function index(Request $request)
    {
        $query = Scholarship::published()->with(['country', 'university']);

        // Basic search
        if ($request->filled('search')) {
            $search = $request->search;
            $query->where(function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%")
                  ->orWhereHas('university', fn($u) => $u->where('name', 'like', "%{$search}%"));
            });
        }

        // Advanced filters from sidebar
        if ($request->filled('country')) {
            $query->where('country_id', $request->country);
        }
        if ($request->filled('level')) {
            // level can be an array if using checkboxes; we'll handle single value for now
            $query->where('program_level', $request->level);
        }
        if ($request->filled('amount_min')) {
            $query->where('amount', '>=', $request->amount_min);
        }
        if ($request->filled('amount_max')) {
            $query->where('amount', '<=', $request->amount_max);
        }

        // Sorting
        $sort = $request->get('sort', 'deadline');
        $direction = $request->get('direction', 'asc');
        $allowedSorts = ['deadline', 'amount', 'title'];
        if (in_array($sort, $allowedSorts)) {
            $query->orderBy($sort, $direction);
        }

        $scholarships = $query->paginate(6)->appends($request->except('page'));

        $countries = Country::orderBy('name')->get();
        $levels = ['bachelor' => 'Bachelor', 'master' => 'Master', 'phd' => 'PhD'];
        
        // Price range values (for slider)
        $minAmount = Scholarship::published()->min('amount') ?? 0;
        $maxAmount = Scholarship::published()->max('amount') ?? 100000;
        $currentMin = $request->input('amount_min', $minAmount);
        $currentMax = $request->input('amount_max', $maxAmount);

        return view('scholarships.index', compact(
            'scholarships', 'countries', 'levels',
            'minAmount', 'maxAmount', 'currentMin', 'currentMax'
        ));
    }
    public function show($slug)
    {
            $scholarship = Scholarship::published()
                ->with(['country', 'university', 'exams'])
                ->where('slug', $slug)
                ->firstOrFail();

            $applicationProgram = $scholarship->university?->programs()
                ->where('is_active', true)
                ->orderBy('application_deadline')
                ->first();

            // Related scholarships: same country or same university
            $related = Scholarship::published()
                ->where('id', '!=', $scholarship->id)
                ->where(function ($q) use ($scholarship) {
                    $q->when($scholarship->country_id, fn($qq) => $qq->orWhere('country_id', $scholarship->country_id));
                    $q->when($scholarship->university_id, fn($qq) => $qq->orWhere('university_id', $scholarship->university_id));
                })
                ->with('country', 'university')
                ->limit(6)
                ->get();

            return view('scholarships.show', compact('scholarship', 'related', 'applicationProgram'));
    }
}
