<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreCareerRequest;
use App\Http\Requests\StoreContactUsRequest;
use App\Models\About;
use App\Models\CareerRequest;
use App\Models\Contact;
use App\Models\ContactRequest;
use App\Models\Event;
use App\Models\Facility;
use App\Models\HomeContent;
use App\Models\News;
use App\Models\OurBoard;
use App\Models\Page;
use App\Models\School;
use App\Models\Setting;
use App\Models\Slider;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;

class FrontController extends Controller
{
    public function index()
    {
        $page = Page::where('slug', '')->first();
        $sliders = Slider::all();
        $schools = School::select('id', 'logo', 'name_en', 'name_ar', 'short_description_en', 'short_description_ar', 'slug')->get();
        $facilities = Facility::latest()->take(6)->get();
        $news = News::latest()->take(3)->get();
        $home_content = HomeContent::first();
        $contact = Contact::first();
        return view('front.pages.index', compact('page', 'sliders', 'schools', 'facilities', 'news', 'home_content', 'contact'));
    }

    public function about()
    {
        $page = Page::where('slug', 'about')->first();
        $about = About::first();
        $our_boards = OurBoard::all();
        $phone = Contact::first()->phone;

        return view('front.pages.about', compact('page', 'about', 'phone', 'our_boards'));
    }


    public function school($slug)
    {
        $school = School::where('slug', $slug)->firstOrFail();
        return view('front.pages.school', compact('school'));
    }

    public function facilities()
    {
        $page = Page::where('slug', 'fasilities')->first();
        $facilities = Facility::all();

        return view('front.pages.fasilities', compact('page', 'facilities'));
    }

    public function careers()
    {
        $page = Page::where('slug', 'careers')->first();
        $positions = Setting::select('career_positions_en', 'career_positions_ar')->first();
        return view('front.pages.careers', compact('page', 'positions'));
    }

    public function submitCareer(StoreCareerRequest $request)
    {
        // Upload resume
        $resumePath = $request->file('resume')->store('resumes', 'public');

        // Save to DB
        CareerRequest::create([
            'name'     => $request->full_name,
            'email'    => $request->email,
            'phone'    => $request->phone,
            'position' => $request->position,
            'resume'   => $resumePath,
            'subject'  => $request->subject,
        ]);

        return back()->with('success', __("Your application has been submitted successfully."));
    }

    public function contactForm()
    {
        $page = Page::where('slug', 'contact-us')->first();
        $contact = Contact::first();

        return view('front.pages.contact', compact('page', 'contact'));
    }

    public function submitContact(StoreContactUsRequest $request)
    {
        $data = $request->validated();

        ContactRequest::create($data);


        return back()->with('success', __("We received your message and you'll hear from us soon. Thank You!"));
    }

    public function news()
    {
        $page = Page::where('slug', 'news')->first();
        $news = News::latest()->get();

        return view('front.pages.news', compact('page', 'news'));
    }

    public function singleNew(News $news)
    {
        return view('front.pages.single.new', compact('news'));
    }



    // In your controller (e.g., the same one with the events() method)
    public function apiEvents(Request $request)
    {
        $month = $request->query('month'); // Expected format: YYYY-MM

        if (!$month || !preg_match('/^\d{4}-\d{2}$/', $month)) {
            return response()->json([]); // Or handle invalid input
        }

        $start = Carbon::parse($month . '-01')->startOfMonth();
        $end = $start->copy()->endOfMonth();

        $events = Event::whereBetween('date', [$start, $end])->get();

        return $events->map(function ($e) {
            return [
                'id' => $e->id,
                'title' => $e->title,
                'date' => $e->date, // String in YYYY-MM-DD format
                'description' => $e->description,
                'image' => $e->image ? asset('storage/' . $e->image) : null, // Adjust path if image storage differs
            ];
        });
    }

    // Also, update your existing events() method to remove unused $events fetch since we're using AJAX now
    public function events()
    {
        $page = Page::where('slug', 'events')->first();
        return view('front.pages.events', compact('page'));
    }
}
