<?php

namespace Blog\Controller;

use Blog\Model\EventModel;
use Blog\Model\StandortModel;

class EventController {

    private $model;
    private $view;

    public function __construct($view) {
        $this->model = new EventModel();
        $this->view = $view;
    }

    public function showEvents() {
        $events = $this->model->getEvents();
        $this->view->setVars([
            'events' => $events
        ]);
    }

    public function createEvent() {
        $data = [
            'name' => $_POST['name'] ?? null,
            'beschreibung' => $_POST['beschreibung'] ?? null,
            'standortid' => $_POST['standortid'] ?? null,
            'datum_von' => $_POST['datum_von'] ?? null,
            'datum_bis' => $_POST['datum_bis'] ?? null,
            'max_tickets' => $_POST['max_tickets'] ?? null,
            'preis' => $_POST['preis'] ?? null
        ];

        $this->model->createEvent($data);
        $this->view->setVars(['event' => $data]);
        exit;
    }

    public function editEventForm() {
        $id = $_GET['ausstellungid'];
        $event = $this->model->getEvent($id);
        $this->view->setVars(['event' => $event]);
    }

    public function updateEvent($id, $data) {
        $id = $_POST['ausstellungid'];
        $data = [
            'standortid' => $_POST['standortid'] ?? null,
            'datum_von' => $_POST['datum_von'] ?? null,
            'datum_bis' => $_POST['datum_bis'] ?? null,
            'name' => $_POST['name'] ?? null,
            'beschreibung' => $_POST['beschreibung'] ?? null,
            'max_tickets' => $_POST['max_tickets'] ?? null,
            'preis' => $_POST['preis'] ?? null
        ];
        $this->model->updateEvent($id, $data);
    }

    public function deleteEvent($id) {
        $this->model->deleteEvent($id);
        $this->view->setVars(['id' => $id]);
    }

    public function showUpdateEvent() {
        $id = $_GET['event_id'] ?? null;
        if (!$id) {
            // handle error, e.g., redirect or show error message
            $this->view->setVars(['error' => 'Keine Event-ID angegeben.']);
            return;
        }
        $event = $this->model->getEvent($id);
        if (!$event) {
            $this->view->setVars(['error' => 'Event nicht gefunden.']);
            return;
        }
        // Map DB fields to view fields if needed
        $eventView = [
            'id' => $event['event_id'],
            'name' => $event['name'],
            'start_date' => $event['start_date'],
            'end_date' => $event['end_date'],
            'location_id' => $event['location_id'],
            'description' => $event['description'],
            'max_tickets' => $event['max_tickets'],
            'ticket_price' => $event['ticket_price'],
        ];
        // Fetch location name (city)
        $standortModel = new StandortModel();
        $location = $standortModel->getStandort($event['location_id']);
        $eventView['location_name'] = $location['city'] ?? '';
        $this->view->setVars(['event' => $eventView]);
    }

    public function showCreateEvent() {
        $standortModel = new \Blog\Model\StandortModel();
        $locations = $standortModel->getStandorte();
        $this->view->setVars(['locations' => $locations]);
    }
}