<?php

namespace App\Controllers;

use App\Models\GalleryModel;
use App\Models\MenuPackageModel;
use App\Models\InquiryModel;

class Home extends BaseController
{
    public function index()
    {
        $serviceModel = new \App\Models\ServiceModel();
        $services = $serviceModel->orderBy('sort_order', 'ASC')->findAll();

        return view('index', [
            'activePage' => 'home',
            'title'      => 'On Call | Quality Self &bull; Rich & Royal | Premium Catering & Events',
            'services'   => $services,
        ]);
    }

    public function services()
    {
        $serviceModel = new \App\Models\ServiceModel();
        $services = $serviceModel->orderBy('sort_order', 'ASC')->findAll();

        return view('services', [
            'activePage' => 'services',
            'title'      => 'Our Services | On Call',
            'services'   => $services,
        ]);
    }

    public function about()
    {
        return view('about', [
            'activePage' => 'about',
            'title'      => 'Our Story | On Call',
        ]);
    }

    public function contact()
    {
        return view('contact', [
            'activePage' => 'contact',
            'title'      => 'Contact Us | On Call',
        ]);
    }

    public function gallery()
    {
        $galleryModel = new GalleryModel();
        $items = $galleryModel->orderBy('id', 'DESC')->findAll();

        return view('gallery', [
            'activePage'   => 'gallery',
            'title'        => 'Gallery | On Call',
            'galleryItems' => $items,
        ]);
    }

    public function menu_planner()
    {
        $menuPackageModel = new MenuPackageModel();
        $packages = $menuPackageModel->findAll();

        // Format for Javascript code compatibility
        $formatted = [];
        foreach ($packages as $pkg) {
            $formatted[] = [
                'id'          => 'sp' . $pkg['id'],
                'name'        => $pkg['name'],
                'category'    => $pkg['category'] ?? 'Lunch',
                'price'       => (int) $pkg['price'],
                'description' => $pkg['description'],
                'items'       => json_decode($pkg['items']),
                'services'    => json_decode($pkg['services'] ?? '[]'),
            ];
        }

        return view('menu-planner', [
            'activePage'         => 'menu-planner',
            'title'              => 'Menu | On Call',
            'sadhyaPackagesJson' => json_encode($formatted),
        ]);
    }

    public function submit_inquiry()
    {
        $inquiryModel = new InquiryModel();

        $data = [
            'name'       => $this->request->getPost('name'),
            'email'      => $this->request->getPost('email'),
            'phone'      => $this->request->getPost('phone'),
            'event_date' => $this->request->getPost('eventDate'),
            'event_type' => $this->request->getPost('eventType'),
            'guests'     => $this->request->getPost('guests'),
            'message'    => $this->request->getPost('message'),
            'created_at' => date('Y-m-d H:i:s'),
        ];

        if (empty($data['name']) || empty($data['email']) || empty($data['phone']) || empty($data['event_date'])) {
            return $this->response->setJSON(['success' => false, 'message' => 'Please fill in all required fields.']);
        }

        if ($inquiryModel->insert($data)) {
            return $this->response->setJSON(['success' => true]);
        }

        return $this->response->setJSON(['success' => false, 'message' => 'Failed to save inquiry. Please try again.']);
    }
}
