<?php

namespace App\Controllers;

use PDO;

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

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

        $conferenceId =
            TenantContext::conferenceId();

        $stmt = $db->prepare("
            SELECT *
            FROM conference_homepage_blocks
            WHERE conference_id = ?
            AND block_type = 'hero_slide'
            ORDER BY sort_order ASC, id ASC
        ");

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

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

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

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

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

        $conferenceId =
            TenantContext::conferenceId();

        $imagePath = null;

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

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

            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/hero/' . $name;
        }

        $stmt = $db->prepare("
            INSERT INTO conference_homepage_blocks
            (
                conference_id,
                block_type,
                title_en,
                title_ar,
                body_en,
                body_ar,
                image_path,
                button_text_en,
                button_text_ar,
                button_url,
                sort_order,
                is_active,
                block_key
            )
            VALUES
            (
                ?, 'hero_slide',
                ?, ?, ?, ?, ?, ?, ?, ?,
                0, 1,
                ?
            )
        ");

        $stmt->execute([

            $conferenceId,

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

            $_POST['body_en'],
            $_POST['body_ar'],

            $imagePath,

            $_POST['button_text_en'],
            $_POST['button_text_ar'],

            $_POST['button_url'],

            'hero_' . time()
        ]);

        $_SESSION['success'] =
            'Hero slide created';

        header(
            'Location: /easychair/admin/hero-slides'
        );

        exit;
    }

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

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

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

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

        $_SESSION['success'] =
            'Hero slide deleted';

        header(
            'Location: /easychair/admin/hero-slides'
        );

        exit;
    }
}
