<?php

declare(strict_types=1);



class MailTypes
{
     
    public static function all(): array
    {
        return [
            'password_reset' => [
                'label'    => 'Password reset',
                'audience' => 'Visitor',
                'desc'     => 'Sent to a user who requested a password reset. Disabling it prevents password recovery by email.',
                'subject'  => '[{site_name}] ' . __('reset_password'),
                'body'     => self::passwordResetBody(),
                'placeholders' => ['{site_name}', '{username}', '{reset_url}', '{primary_color}'],
                'default_enabled' => '1',
                'lock_enabled'    => false,
            ],
            'contact_forward' => [
                'label'    => 'Contact form notification',
                'audience' => 'Admin',
                'desc'     => 'Sent to your contact email whenever a visitor submits the contact form.',
                'subject'  => '[{site_name}] {subject}',
                'body'     => self::contactBody(),
                'placeholders' => ['{site_name}', '{name}', '{email}', '{subject}', '{subject_type}', '{message}', '{content_urls}', '{ip}'],
                'default_enabled' => '1',
                'lock_enabled'    => false,
            ],
            'report_notification' => [
                'label'    => 'Content report notification',
                'audience' => 'Admin',
                'desc'     => 'Optional email to your contact email when a visitor reports a video. Off by default (Telegram is usually enough).',
                'subject'  => '[{site_name}] New content report',
                'body'     => self::reportBody(),
                'placeholders' => ['{site_name}', '{video_title}', '{video_url}', '{reason}', '{details}', '{reporter_email}', '{ip}'],
                'default_enabled' => '0',
                'lock_enabled'    => false,
            ],
        ];
    }

     
    public static function settingKeys(): array
    {
        $keys = [];
        foreach (array_keys(self::all()) as $k) {
            $keys[] = "mail_type_{$k}_enabled";
            $keys[] = "mail_type_{$k}_subject";
            $keys[] = "mail_type_{$k}_body";
        }
        return $keys;
    }

    

    public static function sampleVars(string $key): array
    {
        $site    = (string) Setting::get('site_name', 'TubePress');
        $siteUrl = rtrim((string) Setting::get('site_url', ''), '/') ?: 'https://example.com';
        $primaryRaw = (string) Setting::get('color_primary', '#6366f1');
        $primary = preg_match('/^#[0-9a-fA-F]{3,8}$/', $primaryRaw) ? $primaryRaw : '#6366f1';
        switch ($key) {
            case 'password_reset':
                return ['vars' => [
                    '{site_name}' => $site,
                    '{username}' => 'jane_doe',
                    '{reset_url}' => $siteUrl . '/reset-password?token=2f9c4a1b8e7d6c5a4b3f2e1d0c9b8a7f',
                    '{primary_color}' => $primary,
                ], 'html' => []];
            case 'contact_forward':
                return ['vars' => [
                    '{site_name}' => $site,
                    '{name}' => 'Jane Doe',
                    '{email}' => 'jane@example.com',
                    '{subject}' => 'Question about my account',
                    '{subject_type}' => 'general_inquiry',
                    '{message}' => "Hi there,\n\nI really enjoy your site but had a quick question about my account settings. Could you help me out?\n\nThanks!",
                    '{ip}' => '203.0.113.42',
                ], 'html' => ['{content_urls}' => '']];
            case 'report_notification':
                return ['vars' => [
                    '{site_name}' => $site,
                    '{video_title}' => 'Sample Video Title',
                    '{video_url}' => $siteUrl . '/video/sample-clip',
                    '{reason}' => 'copyright',
                    '{details}' => 'This video appears to use copyrighted material without permission.',
                    '{reporter_email}' => 'reporter@example.com',
                    '{ip}' => '203.0.113.42',
                ], 'html' => []];
        }
        return ['vars' => [], 'html' => []];
    }

    public static function isEnabled(string $key): bool
    {
        $def = self::all()[$key]['default_enabled'] ?? '1';
        return Setting::get("mail_type_{$key}_enabled", $def) === '1';
    }

     
    public static function subject(string $key): string
    {
        $override = trim((string) Setting::get("mail_type_{$key}_subject", ''));
        return $override !== '' ? $override : (string) (self::all()[$key]['subject'] ?? '');
    }

     
    public static function body(string $key): string
    {
        $override = trim((string) Setting::get("mail_type_{$key}_body", ''));
        return $override !== '' ? $override : (string) (self::all()[$key]['body'] ?? '');
    }

     

    private static function passwordResetBody(): string
    {
        $heading = htmlspecialchars(__('reset_password'));
        $btn     = htmlspecialchars(__('reset_password_btn'));
        return '<h2 style="margin:0 0 12px;color:#333;font-size:18px;">' . $heading . '</h2>'
            . '<p style="margin:0 0 20px;color:#555;font-size:15px;line-height:1.6;">A password reset was requested for <strong>{username}</strong>. Click the button below to set a new password:</p>'
            . '<p style="margin:0 0 20px;text-align:center;"><a href="{reset_url}" style="display:inline-block;padding:12px 28px;background-color:{primary_color};color:#ffffff;font-size:15px;font-weight:600;text-decoration:none;border-radius:8px;">' . $btn . '</a></p>'
            . '<p style="margin:0 0 8px;color:#888;font-size:13px;">Or copy this link:</p>'
            . '<p style="margin:0 0 20px;word-break:break-all;font-size:13px;color:{primary_color};">{reset_url}</p>'
            . '<p style="margin:0;color:#999;font-size:12px;">This link expires in 1 hour. If you did not request this, you can ignore this email.</p>';
    }

    private static function contactBody(): string
    {
        return '<h2 style="margin:0 0 12px;color:#333;font-size:18px;">New Contact Message</h2>'
            . '<table style="width:100%;border-collapse:collapse;font-size:14px;color:#555;">'
            . '<tr><td style="padding:8px 0;font-weight:600;color:#333;width:80px;vertical-align:top;">From:</td><td style="padding:8px 0;">{name} &lt;{email}&gt;</td></tr>'
            . '<tr><td style="padding:8px 0;font-weight:600;color:#333;vertical-align:top;">Subject:</td><td style="padding:8px 0;">{subject} <span style="color:#888;">({subject_type})</span></td></tr>'
            . '<tr><td style="padding:8px 0;font-weight:600;color:#333;vertical-align:top;">IP:</td><td style="padding:8px 0;">{ip}</td></tr>'
            . '</table>'
            . '{content_urls}'
            . '<div style="margin:16px 0;padding:16px;background:#f9fafb;border-radius:8px;border:1px solid #e5e7eb;font-size:14px;color:#374151;line-height:1.6;white-space:pre-wrap;">{message}</div>';
    }

    private static function reportBody(): string
    {
        return '<h2 style="margin:0 0 12px;color:#333;font-size:18px;">New Content Report</h2>'
            . '<table style="width:100%;border-collapse:collapse;font-size:14px;color:#555;">'
            . '<tr><td style="padding:8px 0;font-weight:600;color:#333;width:90px;vertical-align:top;">Video:</td><td style="padding:8px 0;"><a href="{video_url}">{video_title}</a></td></tr>'
            . '<tr><td style="padding:8px 0;font-weight:600;color:#333;vertical-align:top;">Reason:</td><td style="padding:8px 0;">{reason}</td></tr>'
            . '<tr><td style="padding:8px 0;font-weight:600;color:#333;vertical-align:top;">Reporter:</td><td style="padding:8px 0;">{reporter_email}</td></tr>'
            . '<tr><td style="padding:8px 0;font-weight:600;color:#333;vertical-align:top;">IP:</td><td style="padding:8px 0;">{ip}</td></tr>'
            . '</table>'
            . '<div style="margin:16px 0;padding:16px;background:#f9fafb;border-radius:8px;border:1px solid #e5e7eb;font-size:14px;color:#374151;line-height:1.6;white-space:pre-wrap;">{details}</div>';
    }
}
