namespace IamService.API.Application.Common;

/// <summary>
/// EN: Standard API response wrapper.
/// VI: Wrapper response API chuẩn.
/// </summary>
/// <typeparam name="T">Type of data in response / Kiểu dữ liệu trong response</typeparam>
public class ApiResponse<T>
{
    /// <summary>
    /// EN: Indicates if the request was successful.
    /// VI: Cho biết request có thành công không.
    /// </summary>
    /// <example>true</example>
    public bool Success { get; set; }

    /// <summary>
    /// EN: Response data.
    /// VI: Dữ liệu response.
    /// </summary>
    public T? Data { get; set; }

    /// <summary>
    /// EN: Error information if request failed.
    /// VI: Thông tin lỗi nếu request thất bại.
    /// </summary>
    public ApiError? Error { get; set; }

    /// <summary>
    /// EN: Pagination information for list responses.
    /// VI: Thông tin phân trang cho list responses.
    /// </summary>
    public PaginationInfo? Pagination { get; set; }

    /// <summary>
    /// EN: Create a successful response.
    /// VI: Tạo response thành công.
    /// </summary>
    public static ApiResponse<T> Ok(T data) => new()
    {
        Success = true,
        Data = data
    };

    /// <summary>
    /// EN: Create a successful paginated response.
    /// VI: Tạo response thành công có phân trang.
    /// </summary>
    public static ApiResponse<T> Ok(T data, PaginationInfo pagination) => new()
    {
        Success = true,
        Data = data,
        Pagination = pagination
    };

    /// <summary>
    /// EN: Create a failed response.
    /// VI: Tạo response thất bại.
    /// </summary>
    public static ApiResponse<T> Fail(string code, string message) => new()
    {
        Success = false,
        Error = new ApiError { Code = code, Message = message }
    };
}

/// <summary>
/// EN: API error details.
/// VI: Chi tiết lỗi API.
/// </summary>
public class ApiError
{
    /// <summary>
    /// EN: Error code for programmatic handling.
    /// VI: Mã lỗi để xử lý lập trình.
    /// </summary>
    /// <example>VALIDATION_ERROR</example>
    public string Code { get; set; } = string.Empty;

    /// <summary>
    /// EN: Human-readable error message.
    /// VI: Thông báo lỗi dễ đọc.
    /// </summary>
    /// <example>The email field is required.</example>
    public string Message { get; set; } = string.Empty;

    /// <summary>
    /// EN: Additional error details.
    /// VI: Chi tiết lỗi bổ sung.
    /// </summary>
    public IDictionary<string, string[]>? Details { get; set; }
}

/// <summary>
/// EN: Pagination information.
/// VI: Thông tin phân trang.
/// </summary>
public class PaginationInfo
{
    /// <summary>
    /// EN: Current page number (1-based).
    /// VI: Số trang hiện tại (bắt đầu từ 1).
    /// </summary>
    /// <example>1</example>
    public int PageNumber { get; set; }

    /// <summary>
    /// EN: Number of items per page.
    /// VI: Số items mỗi trang.
    /// </summary>
    /// <example>10</example>
    public int PageSize { get; set; }

    /// <summary>
    /// EN: Total number of items.
    /// VI: Tổng số items.
    /// </summary>
    /// <example>100</example>
    public int TotalCount { get; set; }

    /// <summary>
    /// EN: Total number of pages.
    /// VI: Tổng số trang.
    /// </summary>
    /// <example>10</example>
    public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
}

/// <summary>
/// EN: OAuth2 Token Response (RFC 6749).
/// VI: OAuth2 Token Response (RFC 6749).
/// </summary>
public class TokenResponse
{
    /// <summary>
    /// EN: The access token issued by the authorization server.
    /// VI: Access token được cấp bởi authorization server.
    /// </summary>
    /// <example>eyJhbGciOiJSUzI1NiIsInR5cCI6ImF0K2p3dCJ9...</example>
    public string AccessToken { get; set; } = string.Empty;

    /// <summary>
    /// EN: The type of the token issued (always "Bearer").
    /// VI: Loại token được cấp (luôn là "Bearer").
    /// </summary>
    /// <example>Bearer</example>
    public string TokenType { get; set; } = "Bearer";

    /// <summary>
    /// EN: The lifetime in seconds of the access token.
    /// VI: Thời gian sống (giây) của access token.
    /// </summary>
    /// <example>3600</example>
    public int ExpiresIn { get; set; }

    /// <summary>
    /// EN: The refresh token (if requested).
    /// VI: Refresh token (nếu được yêu cầu).
    /// </summary>
    /// <example>CfDJ8NrU3...</example>
    public string? RefreshToken { get; set; }

    /// <summary>
    /// EN: The scope of the access token.
    /// VI: Scope của access token.
    /// </summary>
    /// <example>openid profile email roles api</example>
    public string? Scope { get; set; }

    /// <summary>
    /// EN: The ID token (if openid scope requested).
    /// VI: ID token (nếu openid scope được yêu cầu).
    /// </summary>
    public string? IdToken { get; set; }
}
