namespace TaxBaik.Application.Services; using System.Text.RegularExpressions; using TaxBaik.Domain.Entities; using TaxBaik.Domain.Enums; using TaxBaik.Domain.Interfaces; public class InquiryService(IInquiryRepository repository) { private static readonly Regex PhoneRegex = new(@"^01[0-9]-\d{3,4}-\d{4}$"); public async Task SubmitAsync( string name, string phone, string serviceType, string message, string? email = null, string? ipAddress = null, CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(name)) throw new ValidationException("이름을 입력하세요."); if (!PhoneRegex.IsMatch(phone)) throw new ValidationException("올바른 전화번호를 입력하세요. (예: 010-1234-5678)"); if (string.IsNullOrWhiteSpace(message)) throw new ValidationException("문의 내용을 입력하세요."); var inquiry = new Inquiry { Name = name.Trim(), Phone = phone.Trim(), Email = string.IsNullOrWhiteSpace(email) ? null : email.Trim(), ServiceType = serviceType ?? "기타", Message = message.Trim(), IpAddress = ipAddress, Status = InquiryStatusMapper.ToStorageValue(InquiryStatus.New), CreatedAt = DateTime.UtcNow }; return await repository.CreateAsync(inquiry, ct); } public async Task GetByIdAsync(int id, CancellationToken ct = default) => await repository.GetByIdAsync(id, ct); public async Task<(IEnumerable, int)> GetPagedAsync( int page, int pageSize, string? status = null, CancellationToken ct = default) => await repository.GetPagedAsync(NormalizePage(page), NormalizePageSize(pageSize), NormalizeOptionalStatus(status), ct); public async Task UpdateStatusAsync(int id, string status, CancellationToken ct = default) { if (!InquiryStatusMapper.TryParse(status, out var parsed)) throw new ValidationException("지원하지 않는 문의 상태입니다."); await repository.UpdateStatusAsync(id, InquiryStatusMapper.ToStorageValue(parsed), ct); } private static int NormalizePage(int page) => Math.Max(1, page); private static int NormalizePageSize(int pageSize) => Math.Clamp(pageSize, 1, 100); private static string? NormalizeOptionalStatus(string? status) { if (string.IsNullOrWhiteSpace(status)) return null; if (!InquiryStatusMapper.TryParse(status, out var parsed)) throw new ValidationException("지원하지 않는 문의 상태입니다."); return InquiryStatusMapper.ToStorageValue(parsed); } } public class ValidationException : Exception { public ValidationException(string message) : base(message) { } }