<?php

namespace Blog\Model;

use PDOException;

class EventModel extends Database {

    public function getEvents() {
        $pdo = $this->linkDB();
        $sql = "SELECT * FROM event ORDER BY start_date DESC;";
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute();
            return $sth->fetchAll(\PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Lesen der Events.", $e);
            die;
        }
    }

    public function getEvent($id) {
        $pdo = $this->linkDB();
        $sql = "SELECT * FROM event WHERE event_id = :id;";
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute([":id" => $id]);
            return $sth->fetch(\PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Lesen des Events.", $e);
            die;
        }
    }

    public function updateEvent($id, $data) {
        $pdo = $this->linkDB();
        $sql = "UPDATE event SET location_id = :location_id, start_date = :start_date, end_date = :end_date, name = :name, description = :description, max_tickets = :max_tickets, ticket_price = :ticket_price WHERE event_id = :id;";
        $params = [
            ":location_id" => $data['location_id'],
            ":start_date" => $data['start_date'],
            ":end_date" => $data['end_date'],
            ":name" => $data['name'],
            ":description" => $data['description'],
            ":max_tickets" => $data['max_tickets'],
            ":ticket_price" => $data['ticket_price'],
            ":id" => $id
        ];
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute($params);
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Aktualisieren des Events.", $e);
            die;
        }
    }

    public function createEvent($data) {
        $pdo = $this->linkDB();
        $sql = "INSERT INTO event (location_id, start_date, end_date, name, description, max_tickets, ticket_price) VALUES (:location_id, :start_date, :end_date, :name, :description, :max_tickets, :ticket_price);";
        $params = [
            ":location_id" => $data['location_id'],
            ":start_date" => $data['start_date'],
            ":end_date" => $data['end_date'],
            ":name" => $data['name'],
            ":description" => $data['description'],
            ":max_tickets" => $data['max_tickets'],
            ":ticket_price" => $data['ticket_price']
        ];
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute($params);
            return $sth;
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Erstellen des Events.", $e);
            die;
        }
    }

    public function deleteEvent($id) {
        $pdo = $this->linkDB();
        $sql = "DELETE FROM event WHERE event_id = :id;";
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute([":id" => $id]);
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Löschen des Events.", $e);
            die;
        }
    }
}