<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Templates;

use PpPhp\Core\Designer\DesignerComponent as C;
use PpPhp\Core\Designer\DesignerTemplate;

/**
 * Profile page templates — a single rich `account_profile` component rendered
 * in one of four layouts:
 *   card  — clean centered identity card
 *   cover — banner hero with an overlapping avatar and info grid
 *   split — summary sidebar next to the edit form
 *   verdant — scenic frosted identity card
 * Unlike login/register, the profile renders inside the application shell.
 */
final class ProfilePageTemplate extends DesignerTemplate
{
    private const VARIANTS = ['card', 'cover', 'split', 'verdant'];

    public function toArray(): array
    {
        $variant = strtolower((string) ($this->options['variant'] ?? 'card'));
        if (!in_array($variant, self::VARIANTS, true)) {
            $variant = 'card';
        }

        $props = array_replace($this->baseProps(), $this->variantProps($variant));
        $page = $this->basePage($props['title'], [
            C::make('profile_account', 'account_profile', 'Account Profile', $props, [], [], [
                'onLoad' => 'profile:load',
                'server' => 'profile:update',
                'context' => 'global',
            ], [], [
                'designerRuntime' => 'auth',
                'template' => $variant,
            ]),
        ]);

        $page['pageType'] = 'profile';
        $page['table'] = '';
        $page['settings']['profileTemplate'] = $variant;

        return $page;
    }

    private function baseProps(): array
    {
        return [
            'pageType' => 'profile',
            'template' => 'card',
            'title' => 'My profile',
            'subtitle' => 'Your account details and preferences.',
            'accentColor' => '#2563eb',
            'coverColor' => '#0f172a',
            'showAvatar' => true,
            'avatarShape' => 'circle',
            'showRole' => true,
            'showEmail' => true,
            'showUsername' => true,
            'showJoined' => true,
            'nameLabel' => 'Full name',
            'namePlaceholder' => 'Your display name',
            'saveLabel' => 'Save changes',
            'showPasswordLink' => true,
            'passwordLinkLabel' => 'Change password',
            'showLogoutLink' => false,
            'logoutLinkLabel' => 'Sign out',
        ];
    }

    private function variantProps(string $variant): array
    {
        return match ($variant) {
            'cover' => [
                'template' => 'cover',
                'subtitle' => 'Everything about your account, at a glance.',
                'coverColor' => '#312e81',
                'accentColor' => '#6d5dfc',
                'avatarShape' => 'rounded',
            ],
            'split' => [
                'template' => 'split',
                'subtitle' => 'Review your identity and keep details up to date.',
                'accentColor' => '#0f766e',
                'showLogoutLink' => true,
            ],
            'verdant' => [
                'template' => 'verdant',
                'title' => 'Your account',
                'subtitle' => 'Keep your identity and preferences up to date.',
                'accentColor' => '#38c96b',
                'coverColor' => '#6d9078',
                'avatarShape' => 'rounded',
                'showLogoutLink' => true,
            ],
            default => [],
        };
    }
}
