<?php

namespace App\Controllers;
use App\Models\UserModel;
use App\Models\FlightModel;
use App\Models\HotelModel;

class Admin extends BaseController
{
    private function checkAdmin()
    {
        if (!session()->get('isLoggedIn') || session()->get('role') !== 'admin') {
            session()->setFlashdata('error', 'Unauthorized access.');
            return false;
        }
        return true;
    }

    public function index()
    {
        if (!$this->checkAdmin()) return redirect()->to('/dashboard');
        
        $userModel = new UserModel();
        $flightModel = new FlightModel();
        
        // Let's just catch errors if DB isn't setup
        try {
            $data['total_users'] = count($userModel->findAll());
            $data['total_flights'] = count($flightModel->findAll());
        } catch (\Exception $e) {
            $data['total_users'] = 0;
            $data['total_flights'] = 0;
            $data['db_error'] = true;
        }

        $data['title'] = 'Admin Control Panel';
        return view('admin/index', $data);
    }

    public function users()
    {
        if (!$this->checkAdmin()) return redirect()->to('/dashboard');
        
        $userModel = new UserModel();
        try {
            $data['users'] = $userModel->findAll();
        } catch (\Exception $e) {
            $data['users'] = [];
            session()->setFlashdata('error', 'Database not initialized. Please run /setup first.');
        }

        $data['title'] = 'Manage Users';
        return view('admin/users', $data);
    }

    public function flights()
    {
        if (!$this->checkAdmin()) return redirect()->to('/dashboard');
        
        $flightModel = new FlightModel();
        try {
            $data['flights'] = $flightModel->findAll();
        } catch (\Exception $e) {
            $data['flights'] = [];
        }

        $data['title'] = 'Manage Flights';
        return view('admin/flights', $data);
    }
    
    public function addFlight()
    {
        if (!$this->checkAdmin()) return redirect()->to('/dashboard');
        
        $flightModel = new FlightModel();
        
        $data = [
            'airline' => $this->request->getPost('airline'),
            'flight_number' => $this->request->getPost('flight_number'),
            'source' => $this->request->getPost('source'),
            'destination' => $this->request->getPost('destination'),
            'departure_time' => $this->request->getPost('departure_time'),
            'arrival_time' => $this->request->getPost('arrival_time'),
            'base_price' => $this->request->getPost('base_price'),
            'seats_available' => $this->request->getPost('seats_available') ?? 60
        ];
        
        $flightModel->insert($data);
        session()->setFlashdata('success', 'Flight added successfully.');
        return redirect()->to('/admin/flights');
    }

    public function hotels()
    {
        if (!$this->checkAdmin()) return redirect()->to('/dashboard');
        $data['title'] = 'Manage Hotels';
        return view('admin/hotels', $data);
    }
}
