<?php
/*******************************************************************************************************************
| Polls Management Class
| Handles creating and managing polls for community engagement
|*******************************************************************************************************************/

defined('_ISVALID') or header('Location: /error');

class VPolls
{
    public static function createPoll($usr_id, $question, $options, $duration_days = null)
    {
        global $class_database;

        $expires_at = $duration_days ? date('Y-m-d H:i:s', strtotime("+{$duration_days} days")) : null;

        $sql = "INSERT INTO `db_polls` (`usr_id`, `question`, `expires_at`)
                VALUES (%d, '%s', %s)";

        $class_database->doQuery($sql,
            $usr_id,
            $class_database->safe_input($question),
            $expires_at ? "'$expires_at'" : 'NULL'
        );

        $poll_id = $class_database->insert_id();

        // Add options
        foreach ($options as $index => $option) {
            $sql = "INSERT INTO `db_poll_options` (`poll_id`, `option_text`, `display_order`)
                    VALUES (%d, '%s', %d)";

            $class_database->doQuery($sql, $poll_id, $class_database->safe_input($option), $index);
        }

        return ['success' => true, 'poll_id' => $poll_id];
    }

    public static function vote($poll_id, $option_id, $usr_id)
    {
        global $class_database;

        // Check if already voted
        $sql = "SELECT COUNT(*) as count FROM `db_poll_votes` WHERE `poll_id` = %d AND `usr_id` = %d";
        $result = $class_database->doQuery($sql, $poll_id, $usr_id);
        $row = $result->fetch_assoc();

        if ($row['count'] > 0) {
            return ['success' => false, 'error' => 'Already voted'];
        }

        // Add vote
        $sql = "INSERT INTO `db_poll_votes` (`poll_id`, `option_id`, `usr_id`) VALUES (%d, %d, %d)";
        $class_database->doQuery($sql, $poll_id, $option_id, $usr_id);

        // Update vote counts
        $sql = "UPDATE `db_poll_options` SET `votes` = (SELECT COUNT(*) FROM `db_poll_votes` WHERE `option_id` = %d)
                WHERE `option_id` = %d";
        $class_database->doQuery($sql, $option_id, $option_id);

        $sql = "UPDATE `db_polls` SET `total_votes` = (SELECT COUNT(*) FROM `db_poll_votes` WHERE `poll_id` = %d)
                WHERE `poll_id` = %d";
        $class_database->doQuery($sql, $poll_id, $poll_id);

        return ['success' => true];
    }

    public static function getResults($poll_id)
    {
        global $class_database;

        $sql = "SELECT p.*, po.option_id, po.option_text, po.votes, po.display_order
                FROM `db_polls` p
                LEFT JOIN `db_poll_options` po ON p.poll_id = po.poll_id
                WHERE p.poll_id = %d
                ORDER BY po.display_order ASC";

        $result = $class_database->doQuery($sql, $poll_id);

        $poll = null;
        $options = [];

        while ($row = $result->fetch_assoc()) {
            if (!$poll) {
                $poll = [
                    'poll_id' => $row['poll_id'],
                    'question' => $row['question'],
                    'total_votes' => $row['total_votes'],
                    'expires_at' => $row['expires_at'],
                    'closed' => $row['closed']
                ];
            }

            if ($row['option_id']) {
                $options[] = [
                    'option_id' => $row['option_id'],
                    'option_text' => $row['option_text'],
                    'votes' => $row['votes'],
                    'percentage' => $poll['total_votes'] > 0 ? round(($row['votes'] / $poll['total_votes']) * 100, 1) : 0
                ];
            }
        }

        $poll['options'] = $options;
        return $poll;
    }

    public static function closePoll($poll_id, $usr_id)
    {
        global $class_database;

        $sql = "UPDATE `db_polls` SET `closed` = 1 WHERE `poll_id` = %d AND `usr_id` = %d";
        $class_database->doQuery($sql, $poll_id, $usr_id);

        return ['success' => true];
    }
}
