<?php

namespace App\Controllers;

use App\Core\Controller;
use App\Helpers\Auth;
use App\Models\CalendarEvent;
use App\Models\AuditLog;

class CalendarController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        Auth::requireLogin();
    }

    public function index()
    {
        $eventModel = new CalendarEvent();
        $events = $eventModel->all();
        $this->view('calendar/index', array('events' => $events));
    }

    public function store()
    {
        $input = file_get_contents('php://input');
        $data = json_decode($input);

        if (!$data) {
            http_response_code(400);
            echo json_encode(array('error' => 'Invalid JSON'));
            return;
        }

        $eventModel = new CalendarEvent();
        $eventModel->create(array(
            'user_id' => Auth::id(),
            'title' => $data->title,
            'description' => isset($data->description) ? $data->description : '',
            'start_date' => $data->start_date,
            'end_date' => isset($data->end_date) ? $data->end_date : null,
            'color' => isset($data->color) ? $data->color : '#4e73df',
            'status' => 'pending',
        ));

        $audit = new AuditLog();
        $audit->log(Auth::id(), 'event.create', 'calendar_events', $eventModel->db->lastInsertId(), "Created event {$data->title}");

        echo json_encode(array('success' => true));
    }

    public function updateEvent($params)
    {
        $id = isset($params['id']) ? $params['id'] : null;
        $input = file_get_contents('php://input');
        $data = json_decode($input);

        $eventModel = new CalendarEvent();
        $event = $eventModel->find($id);

        if (!$event) {
            http_response_code(404);
            echo json_encode(array('error' => 'Event not found'));
            return;
        }

        $updateData = array();
        if (isset($data->title)) $updateData['title'] = $data->title;
        if (isset($data->description)) $updateData['description'] = $data->description;
        if (isset($data->start_date)) $updateData['start_date'] = $data->start_date;
        if (isset($data->end_date)) $updateData['end_date'] = $data->end_date;
        if (isset($data->color)) $updateData['color'] = $data->color;
        if (isset($data->status)) $updateData['status'] = $data->status;

        if (!empty($updateData)) {
            $eventModel->update($id, $updateData);
        }

        $audit = new AuditLog();
        $audit->log(Auth::id(), 'event.update', 'calendar_events', $id, "Updated event {$event->title}");

        echo json_encode(array('success' => true));
    }

    public function destroy($params)
    {
        $id = isset($params['id']) ? $params['id'] : null;
        $eventModel = new CalendarEvent();
        $event = $eventModel->find($id);

        if ($event) {
            $audit = new AuditLog();
            $audit->log(Auth::id(), 'event.delete', 'calendar_events', $id, "Deleted event {$event->title}");
            $eventModel->delete($id);
        }

        echo json_encode(array('success' => true));
    }
}
