using Application.Abstractions.Messaging;
using Application.Abstractions.Data;
using Microsoft.EntityFrameworkCore;
using Application.Abstractions.Cache;

namespace Application.Features.Config.Get;

public sealed class Handler(IAppDbContext db, ICacheService cache) : IQueryHandler<Query, Response?>
{
    public async Task<Response?> Handle(Query request, CancellationToken ct)
    {
        var cached = await cache.GetAsync<Response>(CacheKeys.Config, ct);
        if (cached is not null)
        {
            return cached;
        }

        var config = await db.Config.AsNoTracking().OrderByDescending(x => x.ID).FirstOrDefaultAsync(ct);
        if (config is null)
        {
            return null;
        }

        var response = new Response
        {
            ID = config.ID,
            Basic = new Response.BasicConfigDto
            {
                SiteName = config.Basic.SiteName,
                SiteURL = config.Basic.SiteURL,
                RootID = config.Basic.RootID,
                FromEmail = config.Basic.FromEmail,
                FromName = config.Basic.FromName,
                SmtpServer = config.Basic.SmtpServer,
                SmtpPort = config.Basic.SmtpPort,
                SmtpEnableSSL = config.Basic.SmtpEnableSSL,
                SmtpUsername = config.Basic.SmtpUsername,
                SmtpPassword = config.Basic.SmtpPassword,
                AdminWhiteIPList = config.Basic.AdminWhiteIPList,
                FrontWhiteIPList = config.Basic.FrontWhiteIPList,
                BlockAlertTitle = config.Basic.BlockAlertTitle,
                BlockAlertContent = config.Basic.BlockAlertContent,
                IsMaintenance = config.Basic.IsMaintenance,
                MaintenanceContent = config.Basic.MaintenanceContent
            },

            Images = new Response.ImagesConfigDto
            {
                FaviconPath = config.Images.Favicon,
                LogoSquarePath = config.Images.LogoSquare,
                LogoHorizontalPath = config.Images.LogoHorizontal,
                OgDefaultPath = config.Images.OgDefault,
                TwitterImagePath = config.Images.TwitterImage,
                AppleTouchIconPath = config.Images.AppleTouchIcon,
                AppIcon192Path = config.Images.AppIcon_192,
                AppIcon512Path = config.Images.AppIcon_512
            },

            Meta = new Response.MetaConfigDto
            {
                Keywords = config.Meta.Keywords,
                Description = config.Meta.Description,
                Author = config.Meta.Author,
                Viewport = config.Meta.Viewport,
                ApplicationName = config.Meta.ApplicationName,
                Generator = config.Meta.Generator,
                Robots = config.Meta.Robots,
                Adds = config.Meta.Adds
            },

            Company = new Response.CompanyConfigDto
            {
                Name = config.Company.Name,
                RegNo = config.Company.RegNo,
                Address = config.Company.Address,
                ZipCode = config.Company.ZipCode,
                Owner = config.Company.Owner,
                Tel = config.Company.Tel,
                Fax = config.Company.Fax,
                RetailSaleNo = config.Company.RetailSaleNo,
                AddedSaleNo = config.Company.AddedSaleNo,
                Hosting = config.Company.Hosting,
                AdminName = config.Company.AdminName,
                AdminEmail = config.Company.AdminEmail,
                SiteUrl = config.Company.SiteUrl,
                BankCode = config.Company.BankCode,
                BankOwner = config.Company.BankOwner,
                BankNumber = config.Company.BankNumber
            },

            Account = new Response.AccountConfigDto
            {
                IsRegisterBlock = config.Account.IsRegisterBlock,
                IsRegisterEmailAuth = config.Account.IsRegisterEmailAuth,
                PasswordMinLength = config.Account.PasswordMinLength,
                PasswordUppercaseLength = config.Account.PasswordUppercaseLength,
                PasswordNumbersLength = config.Account.PasswordNumbersLength,
                PasswordSpecialcharsLength = config.Account.PasswordSpecialcharsLength,
                DeniedEmailList = config.Account.DeniedEmailList,
                DeniedNameList = config.Account.DeniedNameList,
                ChangeEmailDay = config.Account.ChangeEmailDay,
                ChangeNameDay = config.Account.ChangeNameDay,
                ChangeSummaryDay = config.Account.ChangeSummaryDay,
                ChangeIntroDay = config.Account.ChangeIntroDay,
                ChangePasswordDay = config.Account.ChangePasswordDay,
                IsLoginEmailVerifiedOnly = config.Account.IsLoginEmailVerifiedOnly,
                MaxLoginTryCount = config.Account.MaxLoginTryCount,
                MaxLoginTryLimitSecond = config.Account.MaxLoginTryLimitSecond
            },

            EmailTemplate = new Response.EmailTemplateConfigDto
            {
                RegisterEmailFormTitle = config.EmailTemplate.RegisterEmailFormTitle,
                RegisterEmailFormContent = config.EmailTemplate.RegisterEmailFormContent,
                RegistrationEmailFormTitle = config.EmailTemplate.RegistrationEmailFormTitle,
                RegistrationEmailFormContent = config.EmailTemplate.RegistrationEmailFormContent,
                ResetPasswordEmailFormTitle = config.EmailTemplate.ResetPasswordEmailFormTitle,
                ResetPasswordEmailFormContent = config.EmailTemplate.ResetPasswordEmailFormContent,
                ChangedPasswordEmailFormTitle = config.EmailTemplate.ChangedPasswordEmailFormTitle,
                ChangedPasswordEmailFormContent = config.EmailTemplate.ChangedPasswordEmailFormContent,
                WithdrawEmailFormTitle = config.EmailTemplate.WithdrawEmailFormTitle,
                WithdrawEmailFormContent = config.EmailTemplate.WithdrawEmailFormContent,
                EmailVerifyFormTitle = config.EmailTemplate.EmailVerifyFormTitle,
                EmailVerifyFormContent = config.EmailTemplate.EmailVerifyFormContent,
                ChangedEmailFormTitle = config.EmailTemplate.ChangedEmailFormTitle,
                ChangedEmailFormContent = config.EmailTemplate.ChangedEmailFormContent
            },

            External = new Response.ExternalApiConfigDto
            {
                YouTubeApiKeyEnc = config.External.YouTubeApiKeyEnc,
                YouTubeApiName = config.External.YouTubeApiName,
                GoogleClientId = config.External.GoogleClientId,
                GoogleClientSecretEnc = config.External.GoogleClientSecretEnc,
                GoogleAppId = config.External.GoogleAppId
            },

            Payment = new Response.PaymentConfigDto
            {

            }
        };

        await cache.SetAsync(CacheKeys.Config, response, ct);
        return response;
    }
}