<?php

namespace App\Controllers;

require_once BASE_PATH . '/app/helpers/permissions.php';

use PDO;

use App\Core\Controller;
use App\Core\Database;
use App\Core\TenantContext;

class AdminVenueController extends Controller
{
    public function index()
    {
        $db = Database::getInstance();

        $conferenceId =
            TenantContext::conferenceId();

        $stmt = $db->prepare("
            SELECT *
            FROM conference_venues
            WHERE conference_id = ?
            ORDER BY id DESC
        ");

        $stmt->execute([
            $conferenceId
        ]);

        $venues =
            $stmt->fetchAll(PDO::FETCH_ASSOC);

        $this->render(
            'admin/venues/index',
            compact('venues')
        );
    }

    public function create()
    {
        $this->render(
            'admin/venues/create'
        );
    }

    public function store()
    {
        $db = Database::getInstance();

        $conferenceId =
            TenantContext::conferenceId();

        $imagePath = null;

        if (!empty($_FILES['image']['tmp_name'])) {

            $dir =
                BASE_PATH .
                '/public/uploads/venues';

            if (!is_dir($dir)) {
                mkdir($dir, 0777, true);
            }

            $name =
                time() . '_' .
                basename($_FILES['image']['name']);

            move_uploaded_file(
                $_FILES['image']['tmp_name'],
                $dir . '/' . $name
            );

            $imagePath =
                'uploads/venues/' . $name;
        }

        $stmt = $db->prepare("
            INSERT INTO conference_venues
            (
                conference_id,
                title_en,
                title_ar,
                subtitle_en,
                subtitle_ar,
                venue_name,
                country,
                city,
                address_line,
                google_maps_embed,
                image_path,
                is_active
            )
            VALUES
            (
                ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1
            )
        ");

        $stmt->execute([

            $conferenceId,

            $_POST['title_en'],
            $_POST['title_ar'],

            $_POST['subtitle_en'],
            $_POST['subtitle_ar'],

            $_POST['venue_name'],

            $_POST['country'],
            $_POST['city'],

            $_POST['address_line'],

            $_POST['google_maps_embed'],

            $imagePath
        ]);

        $_SESSION['success'] =
            'Venue created';

        header(
            'Location: /easychair/admin/venues'
        );

        exit;
    }

    public function delete()
    {
        $db = Database::getInstance();

        $id =
            (int)($_GET['id'] ?? 0);

        $stmt = $db->prepare("
            DELETE FROM conference_venues
            WHERE id = ?
            LIMIT 1
        ");

        $stmt->execute([$id]);

        $_SESSION['success'] =
            'Venue deleted';

        header(
            'Location: /easychair/admin/venues'
        );

        exit;
    }
}
