<?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 ConferenceSponsorsController extends Controller
{

    public function index()
    {

        $db = Database::getInstance();

        $conferenceId =
            TenantContext::conferenceId();

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

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

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

        $this->render(
            'admin/conference_sponsors/index',
            [
                'sponsors' => $sponsors
            ]
        );
    }

    public function create()
    {

        $this->render(
            'admin/conference_sponsors/create'
        );
    }

    public function store()
    {

        $db = Database::getInstance();

        $conferenceId =
            TenantContext::conferenceId();

        $logo = null;

        /*
        |--------------------------------------------------------------------------
        | LOGO UPLOAD
        |--------------------------------------------------------------------------
        */

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

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

            if (!is_dir($dir)) {

                mkdir(
                    $dir,
                    0777,
                    true
                );
            }

            $ext =
                pathinfo(
                    $_FILES['logo']['name'],
                    PATHINFO_EXTENSION
                );

            $file =
                'sponsor_' .
                time() .
                '_' .
                rand(1000,9999) .
                '.' .
                $ext;

            move_uploaded_file(
                $_FILES['logo']['tmp_name'],
                $dir . '/' . $file
            );

            $logo =
                'uploads/sponsors/' .
                $file;
        }

        $stmt = $db->prepare("
            INSERT INTO conference_sponsors
            (
                conference_id,

                sponsor_name,

                name_en,
                name_ar,

                sponsor_tier,

                description_en,
                description_ar,

                logo,
                website,

                is_active,
                sort_order
            )
            VALUES
            (
                ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
            )
        ");

        $stmt->execute([

            $conferenceId,

            $_POST['name_en'] ?? '',

            $_POST['name_en'] ?? '',
            $_POST['name_ar'] ?? '',

            $_POST['sponsor_tier'] ?? 'Gold',

            $_POST['description_en'] ?? '',
            $_POST['description_ar'] ?? '',

            $logo,

            $_POST['website'] ?? '',

            1,

            (int)(
                $_POST['sort_order']
                ?? 0
            )
        ]);

        $_SESSION['success'] =
            'Sponsor created successfully';

        header(
            'Location: /easychair/admin/conference-sponsors'
        );

        exit;
    }

    public function delete()
    {

        $db = Database::getInstance();

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

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

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

        $_SESSION['success'] =
            'Sponsor deleted successfully';

        header(
            'Location: /easychair/admin/conference-sponsors'
        );

        exit;
    }

}
