<?php

namespace App\Repositories;

use App\Enums\ProcessGeneralState;
use App\Models\Process;
use Config\Database;
use PDO;

class DashboardComunityRepository
{
    public function getResponsesSummaryByType($userId)
    {
        $db = Database::getConnection();

        $typesStmt = $db->query("SELECT id, description FROM response_types");
        $types = $typesStmt->fetchAll(PDO::FETCH_ASSOC);

        $summary = [];

        foreach ($types as $type) {
            $stmt = $db->prepare("
                SELECT COUNT(*) as count 
                FROM responses 
                INNER JOIN processes ON processes.id = responses.process_id
                WHERE responses.response_type_id = :typeId 
                AND processes.user_id = :userId
            ");
            $stmt->execute([
                ':typeId' => $type['id'],
                ':userId' => $userId
            ]);
            $count = $stmt->fetchColumn();

            $summary[$type['description']] = (int)$count;
        }

        return $summary;
    }


    public function getProcessSummaryByState($userId)
    {
        $states = ProcessGeneralState::getAll();
        $db = Database::getConnection();
        $summary = [];

        foreach ($states as $state) {
            $stmt = $db->prepare("
                SELECT COUNT(*) 
                FROM processes 
                WHERE user_id = :userId AND general_state = :general_state
            ");
            $stmt->execute([
                ':userId' => $userId,
                ':general_state' => $state
            ]);
            $count = $stmt->fetchColumn();

            $summary[$state] = (int)$count;
        }

        return $summary;
    }

    public function getLastProcesses($userId, $limit = 3)
    {
        $db = Database::getConnection();
        $stmt = $db->prepare("
            SELECT *
            FROM processes p
            WHERE p.user_id = :userId
            ORDER BY p.entry_date DESC
            LIMIT :limit
        ");
        $stmt->bindValue(':userId', $userId, PDO::PARAM_INT);
        $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
        $stmt->execute();

        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $processes = [];

        foreach ($rows as $row) {
            $processes[] = new Process($row);
        }

        return $processes;
    }

    public function getLastNotifications($userId, $limit = 3)
    {
        $db = Database::getConnection();
        $stmt = $db->prepare("
            SELECT * 
            FROM notifications 
            WHERE user_id = :userId 
            ORDER BY created_at DESC
            LIMIT :limit
        ");
        $stmt->bindValue(':userId', $userId, PDO::PARAM_INT);
        $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
        $stmt->execute();

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

        return $rows;
        
        // $notifications = [];

        // foreach ($rows as $row) {
        //     $notifications[] = new Notification($row);
        // }

        // return $notifications;
    }
}