<?php

namespace App\Components;

use Core\Component;
use Core\View; // Assuming Core\View is part of your framework

class EmailLayout extends Component
{
    // Default values for common properties
    protected string $logoUrl = APP_URL . '/assets/images/logo-dark.png';
    protected string $headline = 'Important Update';
    protected string $customerName = '';
    protected string $companyName = APP_NAME;
    protected string $content = '';
    protected array $footerLinks = [
        'Privacy Policy' => APP_URL . '/privacy'
    ];

    /**
     * Creates a new instance of the EmailLayout component.
     *
     * @return static
     */
    public static function make(): static
    {
        return new static();
    }

    /**
     * Sets the URL for the company logo.
     *
     * @param string $url
     * @return static
     */
    public function logo(string $url): static
    {
        $this->logoUrl = $url;
        return $this;
    }

    /**
     * Sets the main headline of the email.
     *
     * @param string $text
     * @return static
     */
    public function headline(string $text): static
    {
        $this->headline = $text;
        return $this;
    }

    /**
     * Sets the customer's name for a personalized greeting.
     *
     * @param string $name
     * @return static
     */
    public function customerName(string $name): static
    {
        $this->customerName = $name;
        return $this;
    }

    /**
     * Sets the company name to be displayed in the email body.
     *
     * @param string $name
     * @return static
     */
    public function companyName(string $name): static
    {
        $this->companyName = $name;
        return $this;
    }

    /**
     * Sets the HTML content for the body of the email.
     *
     * @param string $html
     * @return static
     */
    public function content(string $html): static
    {
        $this->content = $html;
        return $this;
    }
    
    /**
     * Sets the links to be displayed in the footer.
     *
     * @param array $links An associative array of link label => URL.
     * @return static
     */
    public function footerLinks(array $links): static
    {
        $this->footerLinks = $links;
        return $this;
    }

    /**
     * Renders the complete HTML email template.
     *
     * @return string
     */
    public function render(): string
    {
        $footerLinksHtml = $this->renderFooterLinks();
        $date = date('Y');

        // The HTML template with placeholders
        $template = <<<HTML
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="x-apple-disable-message-reformatting">
    <title></title>
    <style>
        table, td, div, h1, p {font-family: Arial, sans-serif;}
        @media only screen and (max-width: 600px) {
            .full-width {
                width: 100% !important;
            }
            .mobile-padding {
                padding: 10px !important;
            }
        }
    </style>
</head>
<body style="margin:0;padding:0;word-spacing:normal;background-color:#e9f5ff;">
    <div role="article" aria-roledescription="email" lang="en" style="text-size-adjust:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;">
        <table role="presentation" style="width:100%;border-collapse:collapse;border:0;border-spacing:0;background:#e9f5ff;">
            <tr>
                <td align="center" style="padding:0;">
                    <table role="presentation" style="width:600px;border-collapse:collapse;border:1px solid #cceeff;border-spacing:0;text-align:left; background-color:#ffffff; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
                        <tr>
                            <td align="center" style="padding:40px 0 30px 0;">
                                <img src="{$this->logoUrl}" alt="{$this->companyName} Logo" width="150" style="height:auto;display:block;" />
                                <h1 style="font-size:28px;margin:0;padding-top:20px;color:#0056b3;">{$this->headline}</h1>
                            </td>
                        </tr>
                        <tr>
                            <td style="padding:30px;">
                                {$this->content}
                            </td>
                        </tr>
                        <tr>
                            <td style="padding:20px; background-color:#f8f8ff;">
                                <p style="margin:0;font-size:12px;line-height:18px;color:#999999;text-align:center;">
                                    &copy; {$date} {$this->companyName}. All rights reserved.
                                </p>
                                <p style="margin:0;font-size:12px;line-height:18px;color:#999999;text-align:center;padding-top:5px;">
                                    {$footerLinksHtml}
                                </p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>
HTML;
        return $template;
    }
    
    /**
     * Renders the footer links from the provided data.
     *
     * @return string
     */
    protected function renderFooterLinks(): string
    {
        if (empty($this->footerLinks)) {
            return '';
        }

        $linksHtml = [];
        foreach ($this->footerLinks as $label => $url) {
            $linksHtml[] = '<a href="' . htmlspecialchars($url) . '" style="color:#007bff;text-decoration:none;">' . htmlspecialchars($label) . '</a>';
        }
        
        return implode(' | ', $linksHtml);
    }
}