using CMSMicroservice.Protobuf.Protos.User;
using CMSMicroservice.Protobuf.Protos.City;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.UserCQ.Commands.CreateNewUser;
using CMSMicroservice.Application.UserCQ.Commands.UpdateUser;
using CMSMicroservice.Application.UserCQ.Commands.DeleteUser;
using CMSMicroservice.Application.UserCQ.Queries.GetUser;
using CMSMicroservice.Application.UserCQ.Queries.GetAllUserByFilter;
using CMSMicroservice.Application.UserCQ.Queries.GetJwtToken;
using CMSMicroservice.Application.UserCQ.Queries.AdminGetJwtToken;
using CMSMicroservice.Application.UserCQ.Commands.SetPasswordForUser;
using CMSMicroservice.Application.UserCQ.Commands.RefreshToken;
using CMSMicroservice.Application.UserCQ.Commands.CreateNewOtpToken;
using CMSMicroservice.Application.UserCQ.Commands.VerifyOtpToken;
using CMSMicroservice.Application.UserCQ.Commands.AcceptContract;
using Google.Protobuf.WellKnownTypes;
using System.Collections.Generic;
using System.Linq;
namespace CMSMicroservice.WebApi.Services;
public class UserService : UserContract.UserContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;

    public UserService(IDispatchRequestToCQRS dispatchRequestToCQRS)
    {
        _dispatchRequestToCQRS = dispatchRequestToCQRS;
    }
    public override async Task<CreateNewUserResponse> CreateNewUser(CreateNewUserRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<CreateNewUserRequest, CreateNewUserCommand, CreateNewUserResponse>(request, context);
    }
    public override async Task<Empty> UpdateUser(UpdateUserRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<UpdateUserRequest, UpdateUserCommand>(request, context);
    }
    public override async Task<Empty> DeleteUser(DeleteUserRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<DeleteUserRequest, DeleteUserCommand>(request, context);
    }
    public override async Task<GetUserResponse> GetUser(GetUserRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetUserRequest, GetUserQuery, GetUserResponse>(request, context);
    }
    public override async Task<GetAllUserByFilterResponse> GetAllUserByFilter(GetAllUserByFilterRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetAllUserByFilterRequest, GetAllUserByFilterQuery, GetAllUserByFilterResponse>(request, context);
    }
    public override async Task<GetJwtTokenResponse> GetJwtToken(GetJwtTokenRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetJwtTokenRequest, GetJwtTokenQuery, GetJwtTokenResponse>(request, context);
    }
    public override async Task<AdminGetJwtTokenResponse> AdminGetJwtToken(AdminGetJwtTokenRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<AdminGetJwtTokenRequest, AdminGetJwtTokenQuery, AdminGetJwtTokenResponse>(request, context);
    }
    public override async Task<Empty> SetPasswordForUser(SetPasswordForUserRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<SetPasswordForUserRequest, SetPasswordForUserCommand>(request, context);
    }
    public override async Task<RefreshTokenResponse> RefreshToken(RefreshTokenRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<RefreshTokenRequest, RefreshTokenCommand, RefreshTokenResponse>(request, context);
    }
    
    // ============= Customer-specific Methods =============
    
    public override async Task<CreateNewOtpTokenResponse> CreateNewOtpToken(CreateNewOtpTokenRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<CreateNewOtpTokenRequest, CreateNewOtpTokenCommand, CreateNewOtpTokenResponse>(request, context);
    }
    
    public override async Task<VerifyOtpTokenResponse> VerifyOtpToken(VerifyOtpTokenRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<VerifyOtpTokenRequest, VerifyOtpTokenCommand, VerifyOtpTokenResponse>(request, context);
    }
    
    public override async Task<AcceptContractResponse> AcceptContract(AcceptContractRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<AcceptContractRequest, AcceptContractCommand, AcceptContractResponse>(request, context);
    }
    
    public override async Task<GetUserForCustomerResponse> GetUserForCustomer(GetUserForCustomerRequest request, ServerCallContext context)
    {
        // Mock implementation for Customer Get User
        await Task.Delay(10); // Simulate async operation
        
        return new GetUserForCustomerResponse
        {
            Id = 123,
            FirstName = "احمد",
            LastName = "محمدی",
            Mobile = "09123456789",
            Email = "ahmad.mohammadi@example.com",
            NationalCode = "1234567890",
            AvatarPath = "/avatars/user_123.jpg",
            ParentId = 100,
            ReferralCode = "REF123456",
            IsMobileVerified = true,
            MobileVerifiedAt = Timestamp.FromDateTime(DateTime.SpecifyKind(new DateTime(2024, 1, 15), DateTimeKind.Utc)),
            EmailNotifications = true,
            SmsNotifications = true,
            PushNotifications = false,
            BirthDate = Timestamp.FromDateTime(DateTime.SpecifyKind(new DateTime(1990, 5, 20), DateTimeKind.Utc))
        };
    }

    public override async Task<Empty> UpdateCustomerProfile(UpdateCustomerProfileRequest request, ServerCallContext context)
    {
        // Mock implementation for Update Customer Profile
        await Task.Delay(10);
        return new Empty();
    }

    public override async Task<GetCustomerProfileResponse> GetCustomerProfile(GetCustomerProfileRequest request, ServerCallContext context)
    {
        // Mock implementation for Get Customer Profile
        await Task.Delay(10);
        
        return new GetCustomerProfileResponse
        {
            Id = 123,
            FirstName = "احمد",
            LastName = "محمدی",
            Mobile = "09123456789",
            Email = "ahmad.mohammadi@example.com",
            NationalCode = "1234567890",
            AvatarPath = "/avatars/user_123.jpg",
            ParentId = 100,
            ReferralCode = "REF123456",
            IsMobileVerified = true,
            MobileVerifiedAt = Timestamp.FromDateTime(DateTime.SpecifyKind(new DateTime(2024, 1, 15), DateTimeKind.Utc)),
            EmailNotifications = true,
            SmsNotifications = true,
            PushNotifications = false,
            BirthDate = Timestamp.FromDateTime(DateTime.SpecifyKind(new DateTime(1990, 5, 20), DateTimeKind.Utc)),
            FullName = "احمد محمدی",
            ProfileCompletionPercentage = 85
        };
    }

    public override async Task<ChangeCustomerPasswordResponse> ChangeCustomerPassword(ChangeCustomerPasswordRequest request, ServerCallContext context)
    {
        // Mock implementation for Change Customer Password
        await Task.Delay(10);
        
        if (request.NewPassword != request.ConfirmPassword)
        {
            return new ChangeCustomerPasswordResponse
            {
                Success = false,
                Message = "رمز عبور جدید و تکرار آن یکسان نیستند"
            };
        }

        if (request.NewPassword.Length < 6)
        {
            return new ChangeCustomerPasswordResponse
            {
                Success = false,
                Message = "رمز عبور باید حداقل 6 کاراکتر باشد"
            };
        }

        return new ChangeCustomerPasswordResponse
        {
            Success = true,
            Message = "رمز عبور با موفقیت تغییر یافت"
        };
    }

    public override async Task<GetCustomerReferralsResponse> GetCustomerReferrals(GetCustomerReferralsRequest request, ServerCallContext context)
    {
        // Mock implementation for Get Customer Referrals
        await Task.Delay(10);
        
        var referrals = new List<CustomerReferralModel>
        {
            new CustomerReferralModel
            {
                Id = 1,
                FirstName = "علی",
                LastName = "احمدی",
                Mobile = "09121234567",
                JoinDate = Timestamp.FromDateTime(DateTime.SpecifyKind(new DateTime(2025, 12, 1), DateTimeKind.Utc)),
                IsActive = true,
                StatusMessage = "فعال",
                Level = 1,
                TotalCommission = 2500000
            },
            new CustomerReferralModel
            {
                Id = 2,
                FirstName = "فاطمه",
                LastName = "کریمی",
                Mobile = "09122345678",
                JoinDate = Timestamp.FromDateTime(DateTime.SpecifyKind(new DateTime(2025, 11, 15), DateTimeKind.Utc)),
                IsActive = true,
                StatusMessage = "فعال",
                Level = 1,
                TotalCommission = 1800000
            },
            new CustomerReferralModel
            {
                Id = 3,
                FirstName = "محسن",
                LastName = "رضایی",
                Mobile = "09123456789",
                JoinDate = Timestamp.FromDateTime(DateTime.SpecifyKind(new DateTime(2025, 10, 20), DateTimeKind.Utc)),
                IsActive = false,
                StatusMessage = "غیرفعال",
                Level = 1,
                TotalCommission = 950000
            }
        };

        return new GetCustomerReferralsResponse
        {
            MetaData = new MetaData
            {
                CurrentPage = 1,
                TotalPage = 1,
                PageSize = 10,
                TotalCount = 3,
                HasPrevious = false,
                HasNext = false
            },
            Referrals = { referrals },
            Stats = new CustomerReferralStats
            {
                TotalReferrals = 3,
                ActiveReferrals = 2,
                TotalCommissionEarned = 5250000,
                ThisMonthCommission = 850000
            }
        };
    }

    public override async Task<UploadCustomerAvatarResponse> UploadCustomerAvatar(UploadCustomerAvatarRequest request, ServerCallContext context)
    {
        // Mock implementation for Upload Customer Avatar
        await Task.Delay(10);
        
        if (request.FileData == null || request.FileData.Length == 0)
        {
            return new UploadCustomerAvatarResponse
            {
                Success = false,
                Message = "فایل انتخاب نشده است"
            };
        }

        if (request.FileData.Length > 5 * 1024 * 1024) // 5MB limit
        {
            return new UploadCustomerAvatarResponse
            {
                Success = false,
                Message = "حجم فایل نباید بیش از 5 مگابایت باشد"
            };
        }

        var allowedTypes = new[] { "image/jpeg", "image/jpg", "image/png", "image/gif" };
        if (!allowedTypes.Contains(request.FileMimeType?.ToLower()))
        {
            return new UploadCustomerAvatarResponse
            {
                Success = false,
                Message = "فرمت فایل مجاز نیست. فقط JPG, PNG و GIF مجاز هستند"
            };
        }

        // Simulate file upload and generate URL
        var fileName = $"avatar_{DateTime.Now.Ticks}.{request.FileMimeType?.Split('/').LastOrDefault()}";
        var avatarUrl = $"/uploads/avatars/{fileName}";

        return new UploadCustomerAvatarResponse
        {
            Success = true,
            Message = "تصویر پروفایل با موفقیت آپلود شد",
            AvatarUrl = avatarUrl
        };
    }

    public override async Task<GetCustomerSettingsResponse> GetCustomerSettings(GetCustomerSettingsRequest request, ServerCallContext context)
    {
        // Mock implementation for Get Customer Settings
        await Task.Delay(10);
        
        return new GetCustomerSettingsResponse
        {
            EmailNotifications = true,
            SmsNotifications = true,
            PushNotifications = false,
            MarketingNotifications = true,
            PreferredLanguage = "fa-IR",
            TimeZone = "Asia/Tehran",
            TwoFactorAuthEnabled = false
        };
    }

    public override async Task<Empty> UpdateCustomerSettings(UpdateCustomerSettingsRequest request, ServerCallContext context)
    {
        // Mock implementation for Update Customer Settings
        await Task.Delay(10);
        return new Empty();
    }
}
