<?php

namespace App\Support;

class CacheKeys
{
    protected static function filtersSuffix(array $filters = []): string
    {
        return empty($filters) ? 'default' : md5(json_encode($filters, JSON_UNESCAPED_UNICODE));
    }

    public static function merchantDashboardStats(int $merchantId): string
    {
        return "merchant:{$merchantId}:dashboard:stats";
    }

    public static function merchantProductsVersion(int $merchantId): string
    {
        return "merchant:{$merchantId}:products:version";
    }

    public static function merchantProductsList(int $merchantId, int $page = 1, array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::merchantProductsVersion($merchantId), 1);

        return "merchant:{$merchantId}:products:v{$version}:list:page:{$page}:filters:{$suffix}";
    }

    public static function merchantProductsStatusStats(int $merchantId, array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::merchantProductsVersion($merchantId), 1);

        return "merchant:{$merchantId}:products:v{$version}:status-stats:filters:{$suffix}";
    }

    public static function merchantProductsSummary(int $merchantId, array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::merchantProductsVersion($merchantId), 1);

        return "merchant:{$merchantId}:products:v{$version}:summary:filters:{$suffix}";
    }

    public static function platformDashboardStats(): string
    {
        return 'platform:dashboard:stats';
    }

    public static function platformProductsVersion(): string
    {
        return 'platform:products:version';
    }

    public static function platformMerchantsList(int $page = 1): string
    {
        return "platform:merchants:list:page:{$page}";
    }

    public static function merchantOrdersVersion(int $merchantId): string
    {
        return "merchant:{$merchantId}:orders:version";
    }

    public static function merchantOrdersList(int $merchantId, int $page = 1, array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::merchantOrdersVersion($merchantId), 1);

        return "merchant:{$merchantId}:orders:v{$version}:list:page:{$page}:filters:{$suffix}";
    }

    public static function merchantOrdersStatusStats(int $merchantId, array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::merchantOrdersVersion($merchantId), 1);

        return "merchant:{$merchantId}:orders:v{$version}:status-stats:filters:{$suffix}";
    }

    public static function merchantOrdersSummary(int $merchantId, array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::merchantOrdersVersion($merchantId), 1);

        return "merchant:{$merchantId}:orders:v{$version}:summary:filters:{$suffix}";
    }

    public static function merchantOrdersTrendSummary(int $merchantId, array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::merchantOrdersVersion($merchantId), 1);

        return "merchant:{$merchantId}:orders:v{$version}:trend-summary:filters:{$suffix}";
    }

    public static function platformOrdersVersion(): string
    {
        return 'platform:orders:version';
    }

    public static function platformOrdersList(int $page = 1, array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::platformOrdersVersion(), 1);

        return "platform:orders:v{$version}:list:page:{$page}:filters:{$suffix}";
    }

    public static function platformOrdersStatusStats(array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::platformOrdersVersion(), 1);

        return "platform:orders:v{$version}:status-stats:filters:{$suffix}";
    }

    public static function platformOrdersSummary(array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::platformOrdersVersion(), 1);

        return "platform:orders:v{$version}:summary:filters:{$suffix}";
    }

    public static function platformOrdersTrendSummary(array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::platformOrdersVersion(), 1);

        return "platform:orders:v{$version}:trend-summary:filters:{$suffix}";
    }

    public static function platformProductsList(int $page = 1, array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::platformProductsVersion(), 1);

        return "platform:products:v{$version}:list:page:{$page}:filters:{$suffix}";
    }

    public static function platformProductsStatusStats(array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::platformProductsVersion(), 1);

        return "platform:products:v{$version}:status-stats:filters:{$suffix}";
    }

    public static function platformProductsSummary(array $filters = []): string
    {
        $suffix = self::filtersSuffix($filters);
        $version = (int) cache()->get(self::platformProductsVersion(), 1);

        return "platform:products:v{$version}:summary:filters:{$suffix}";
    }

    public static function merchantCategoriesList(int $merchantId, int $page = 1): string
    {
        return "merchant:{$merchantId}:categories:list:page:{$page}";
    }

    public static function merchantUsersList(int $merchantId, int $page = 1): string
    {
        return "merchant:{$merchantId}:users:list:page:{$page}";
    }

    public static function platformCategoriesList(int $page = 1): string
    {
        return "platform:categories:list:page:{$page}";
    }

    public static function platformSystemConfigs(): string
    {
        return 'platform:settings:system';
    }

    public static function platformChannelsOverview(): string
    {
        return 'platform:settings:channels';
    }
}
