<?php

namespace App\Controllers;

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

class AdminOrganizationController extends Controller
{
    private function db()
    {
        return Database::getInstance();
    }

    public function index()
    {
        $db = $this->db();

        $stmt = $db->query("
            SELECT
                o.*,

                COUNT(DISTINCT c.id) AS conferences_count,

                COUNT(DISTINCT u.id) AS users_count,

                COUNT(
                    DISTINCT CASE
                        WHEN c.start_date <= NOW()
                         AND c.end_date >= NOW()
                        THEN c.id
                    END
                ) AS current_count

            FROM organizations o

            LEFT JOIN conferences c
                ON c.org_id = o.id

            LEFT JOIN users u
                ON u.org_id = o.id

            GROUP BY o.id

            ORDER BY o.id DESC
        ");

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

        $this->render_admin(
            'admin/organizations/index',
            compact('organizations')
        );
    }

    public function create()
    {
        $organization = [];

        $this->render_admin(
            'admin/organizations/form',
            compact('organization')
        );
    }

    public function store()
    {
        $db = $this->db();

        $stmt = $db->prepare("
            INSERT INTO organizations
            (
                name,
                name_ar,
                email,
                phone,
                is_active,
                created_at
            )
            VALUES
            (
                ?,?,?,?,?,NOW()
            )
        ");

        $stmt->execute([

            $_POST['name'],
            $_POST['name_ar'],
            $_POST['email'],
            $_POST['phone'],
            1

        ]);

        header(
            "Location: /easychair/admin/organizations"
        );

        exit;
    }

    public function edit()
    {
        $db = $this->db();

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

        $stmt =
            $db->prepare("
                SELECT *
                FROM organizations
                WHERE id = ?
            ");

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

        $organization =
            $stmt->fetch(\PDO::FETCH_ASSOC);

        $this->render_admin(
            'admin/organizations/form',
            compact('organization')
        );
    }

    public function update()
    {
        $db = $this->db();

        $stmt =
            $db->prepare("
                UPDATE organizations
                SET
                    name = ?,
                    name_ar = ?,
                    email = ?,
                    phone = ?,
                    updated_at = NOW()
                WHERE id = ?
            ");

        $stmt->execute([

            $_POST['name'],
            $_POST['name_ar'],
            $_POST['email'],
            $_POST['phone'],
            $_POST['id']

        ]);

        header(
            "Location: /easychair/admin/organizations"
        );

        exit;
    }
}
