<?php

namespace App\Controllers;

use App\Models\ProjectModel;
use App\Models\ServiceModel;
use App\Models\SettingsModel;
use App\Models\CategoryModel;
use App\Models\GalleryModel;
use App\Models\BannerModel;



class Home extends BaseController
{
    public function index()
    {
        $projectModel = new ProjectModel();
        $serviceModel = new ServiceModel();

        $settingsModel = new SettingsModel();
        $catModel = new CategoryModel();
        $bannerModel = new BannerModel();
        $data = [
            'title' => 'Home',
            'projects' => $projectModel->findAll(3),
            'services' => $serviceModel->orderBy('id', 'ASC')->findAll(6),

            'categories' => $catModel->findAll(6),
            'slider_images' => $bannerModel->orderBy('order_index', 'ASC')->findAll(),
            'about' => [
                'title' => $settingsModel->getSetting('home_about_title') ?? 'Shaping Modern <span style="color: var(--accent);">Landscapes</span> Since 2017',
                'description' => $settingsModel->getSetting('home_about_description') ?? 'Saanvi Infratech was established in 2017 with a vision to deliver world-class engineering solutions. Over the years, we have evolved into a trusted partner for both government and private sector projects.<br><br>We take immense pride in our contribution to national infrastructure, working closely with prestigious organizations like <strong>Rail Vikas Nigam Limited (RVNL)</strong> and other government bodies to deliver large-scale railway and civil projects.',
                'image' => $settingsModel->getSetting('home_about_image') ?? base_url('assets/images/home/about_construction.png'),
                'feature_title' => $settingsModel->getSetting('home_about_feature_title') ?? 'Commitment to Excellence',
                'feature_desc' => $settingsModel->getSetting('home_about_feature_desc') ?? 'Delivering precision engineering with unwavering quality standards.',
            ],
            'counters' => [


                'projects' => $settingsModel->getSetting('projects_completed'),
                'years' => (!empty($settingsModel->getSetting('years_experience'))) ? $settingsModel->getSetting('years_experience') : (date('Y') - ($settingsModel->getSetting('foundation_year') ?? 1999)) . '+',
                'clients' => $settingsModel->getSetting('happy_clients'),
                'active' => $settingsModel->getSetting('active_projects'),
            ]
        ];

        return view('home', $data);
    }

    public function services($id = null)
    {
        $serviceModel = new ServiceModel();
        
        if ($id) {
            $data = [
                'title' => 'Service Details',
                'service' => $serviceModel->find($id)
            ];
            return view('service_details', $data);
        }

        $data = [
            'title' => 'Our Services',
            'services' => $serviceModel->findAll()
        ];
        return view('services', $data);
    }

    public function projects($id = null)
    {
        $projectModel = new ProjectModel();
        
        if ($id) {
            $data = [
                'title' => 'Project Details',
                'project' => $projectModel->find($id)
            ];
            return view('project_details', $data);
        }

        $data = [
            'title' => 'Our Projects',
            'projects' => $projectModel->findAll()
        ];
        return view('projects', $data);
    }

    public function contact()
    {
        $settingsModel = new SettingsModel();
        $data = [
            'title' => 'Contact Us',
            'contact_email' => $settingsModel->getSetting('contact_email'),
            'contact_phone' => $settingsModel->getSetting('contact_phone'),
            'site_address' => $settingsModel->getSetting('site_address'),
            'facebook_url' => $settingsModel->getSetting('facebook_url'),
            'twitter_url' => $settingsModel->getSetting('twitter_url'),
            'linkedin_url' => $settingsModel->getSetting('linkedin_url'),
            'instagram_url' => $settingsModel->getSetting('instagram_url'),
        ];
        return view('contact', $data);
    }


    public function gallery()
    {
        $galleryModel = new GalleryModel();
        $data = [
            'title' => 'Gallery',
            'images' => $galleryModel->where('type', 'image')->findAll(),
            'videos' => $galleryModel->where('type', 'video')->findAll()
        ];
        return view('gallery', $data);
    }

    public function about()
    {
        $settingsModel = new SettingsModel();
        $serviceModel = new \App\Models\ServiceModel();
        $projectModel = new \App\Models\ProjectModel();
        
        $data = [
            'title' => 'About Us',
            'about_title' => $settingsModel->getSetting('about_title') ?? 'Building a Sustainable Future Since 2017',
            'about_description' => $settingsModel->getSetting('about_description') ?? 'Saanvi Infratech is a premier engineering and construction firm dedicated to transforming complex challenges into landmark structures.',
            'about_content' => $settingsModel->getSetting('about_content') ?? 'Founded in 2017, we have quickly risen to become a trusted name in the infrastructure sector. Our journey began with a small, dedicated team of engineers and has expanded into a multi-disciplinary force capable of handling large-scale government and private projects. We specialize in railway infrastructure, residential community development, and modern commercial spaces. Our partnership with Rail Vikas Nigam Limited (RVNL) stands as a testament to our commitment to national growth and engineering excellence.',
            'about_image' => $settingsModel->getSetting('about_image') ?? base_url('assets/images/home/about_construction.png'),
            'mission' => $settingsModel->getSetting('mission_content') ?? 'To deliver superior construction services through innovation, precision engineering, and an uncompromising commitment to quality and safety.',
            'vision' => $settingsModel->getSetting('vision_content') ?? 'To be the most trusted name in infrastructure development, recognized for shaping modern landscapes and building lasting value for the nation.',
            'values' => $settingsModel->getSetting('values_content') ?? 'Integrity, Innovation, Safety, and Excellence are the pillars that support every Saanvi Infratech project.',
            'services' => $serviceModel->findAll(3),
            'projects' => $projectModel->findAll(3),
            'years_experience' => (!empty($settingsModel->getSetting('years_experience'))) ? $settingsModel->getSetting('years_experience') : (date('Y') - ($settingsModel->getSetting('foundation_year') ?? 1999)) . '+',
        ];

        return view('about', $data);
    }
}

