<?php

namespace FleekDash\Services\Theme;

defined('ABSPATH') || exit;

/**
 * PaletteSyncService
 *
 * Maps WordPress admin color selections to FleekDash palettes.
 * Allows enabling/disabling sync per user.
 */
class PaletteSyncService
{
    private const USER_META_SYNC_DISABLED = '_fleekdash_palette_sync_disabled';
    private ThemeService $themeService;

    public function __construct(?ThemeService $themeService = null)
    {
        $this->themeService = $themeService ?? new ThemeService();
    }

    /**
     * Determine recommended palette for a user and theme based on WP admin color.
     *
     * @return array<string, mixed>|null
     */
    public function getSyncedPaletteForUser(int $userId, string $themeId): ?array
    {
        $themeId = \sanitize_key($themeId);
        if ($themeId === '' || $this->isSyncDisabledForUser($userId)) {
            return null;
        }

        $wpPaletteKey = $this->getWordPressAdminPalette($userId);

        if ($wpPaletteKey === '') {
            return null;
        }

        $paletteId = ThemeRegistry::getSyncedPaletteId($themeId, $wpPaletteKey);
        if (!$paletteId) {
            return null;
        }

        $palette = $this->themeService->getPalette($paletteId);
        if (!$palette) {
            return null;
        }

        return [
            'palette_id' => $palette['id'],
            'wp_palette_key' => $wpPaletteKey,
            'palette' => $palette,
            'auto_applied' => true,
        ];
    }

    /**
     * Determine recommended palette for a theme given a WP admin palette key.
     *
     * @return array<string, mixed>|null
     */
    public function getSyncedPalette(string $themeId, string $wpPaletteKey): ?array
    {
        $themeId = \sanitize_key($themeId);
        $wpPaletteKey = \sanitize_key($wpPaletteKey);

        if ($themeId === '' || $wpPaletteKey === '') {
            return null;
        }

        $paletteId = ThemeRegistry::getSyncedPaletteId($themeId, $wpPaletteKey);
        if (!$paletteId) {
            return null;
        }

        return $this->themeService->getPalette($paletteId);
    }

    /**
     * Disable automatic palette sync for a user.
     */
    public function disableSyncForUser(int $userId): void
    {
        \update_user_meta($userId, self::USER_META_SYNC_DISABLED, '1');
    }

    /**
     * Enable automatic palette sync for a user.
     */
    public function enableSyncForUser(int $userId): void
    {
        \delete_user_meta($userId, self::USER_META_SYNC_DISABLED);
    }

    /**
     * Determine if sync is disabled for user.
     */
    public function isSyncDisabledForUser(int $userId): bool
    {
        return \get_user_meta($userId, self::USER_META_SYNC_DISABLED, true) === '1';
    }

    /**
     * Retrieve WordPress admin color key for user.
     */
    private function getWordPressAdminPalette(int $userId): string
    {
        if ($userId <= 0) {
            return '';
        }

        $palette = get_user_option('admin_color', $userId);
        if (!is_string($palette) || $palette === '') {
            $palette = 'fresh'; // WordPress default
        }

        return \sanitize_key($palette);
    }
}
