<?php
/**
 * Pattern Assignment Helper
 * 
 * Static methods to help assign patterns to courses and retrieve pattern info.
 */

namespace SuperAdmin\Controllers;

class PatternAssignmentHelper
{
    /**
     * Get all locked patterns (for dropdowns)
     */
    public static function getLockedPatterns(): array
    {
        try {
            $configFile = dirname(dirname(dirname(__DIR__))) . '/config/database.php';
            if (!file_exists($configFile)) return [];

            $config = include $configFile;
            if (!is_array($config)) return [];

            $dsn = "mysql:host={$config['host']};dbname={$config['dbname']};charset=utf8mb4";
            $pdo = new \PDO($dsn, $config['user'] ?? '', $config['pass'] ?? '', [
                \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
            ]);

            $stmt = $pdo->query("SELECT `id`, `name`, `is_default` FROM `certificate_patterns` WHERE `status` = 'locked' ORDER BY `is_default` DESC, `name` ASC");
            return $stmt->fetchAll() ?: [];
        } catch (\Throwable $e) {
            return [];
        }
    }

    /**
     * Assign a pattern to a course (cert or marksheet)
     */
    public static function assignToCourse(int $courseId, int $patternId, string $docType): bool
    {
        try {
            $configFile = dirname(dirname(dirname(__DIR__))) . '/config/database.php';
            if (!file_exists($configFile)) return false;

            $config = include $configFile;
            $dsn = "mysql:host={$config['host']};dbname={$config['dbname']};charset=utf8mb4";
            $pdo = new \PDO($dsn, $config['user'] ?? '', $config['pass'] ?? '', [
                \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
            ]);

            $col = $docType === 'certificate' ? 'cert_pattern_id' : 'marksheet_pattern_id';
            $stmt = $pdo->prepare("UPDATE `courses` SET `{$col}` = ? WHERE `id` = ?");
            return $stmt->execute([$patternId, $courseId]);
        } catch (\Throwable $e) {
            return false;
        }
    }

    /**
     * Get pattern info for a course
     */
    public static function getCoursePatterns(int $courseId): array
    {
        try {
            $configFile = dirname(dirname(dirname(__DIR__))) . '/config/database.php';
            if (!file_exists($configFile)) return ['cert' => null, 'marksheet' => null];

            $config = include $configFile;
            $dsn = "mysql:host={$config['host']};dbname={$config['dbname']};charset=utf8mb4";
            $pdo = new \PDO($dsn, $config['user'] ?? '', $config['pass'] ?? '', [
                \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
            ]);

            $stmt = $pdo->prepare("
                SELECT c.cert_pattern_id, c.marksheet_pattern_id,
                       cp.name AS cert_pattern_name,
                       mp.name AS marksheet_pattern_name,
                       cp.cert_background_path,
                       mp.marksheet_background_path
                FROM courses c
                LEFT JOIN certificate_patterns cp ON c.cert_pattern_id = cp.id
                LEFT JOIN certificate_patterns mp ON c.marksheet_pattern_id = mp.id
                WHERE c.id = ?
            ");
            $stmt->execute([$courseId]);
            $row = $stmt->fetch();

            if (!$row) return ['cert' => null, 'marksheet' => null];

            return [
                'cert' => [
                    'id' => $row['cert_pattern_id'],
                    'name' => $row['cert_pattern_name'],
                    'bg' => $row['cert_background_path'],
                ],
                'marksheet' => [
                    'id' => $row['marksheet_pattern_id'],
                    'name' => $row['marksheet_pattern_name'],
                    'bg' => $row['marksheet_background_path'],
                ],
            ];
        } catch (\Throwable $e) {
            return ['cert' => null, 'marksheet' => null];
        }
    }
}
