import 'package:json_annotation/json_annotation.dart';
import 'package:networks_sdk/networks_sdk.dart';

import 'package:im_app/core/foundation/api_paths.dart';
import 'package:im_app/domain/entities/user.dart';

part 'login_request.g.dart';

/// # /app/api/auth/login-user — 使用 vcode_token 完成登录
///
/// 流程：发送验证码（[SendOtpRequest]）→ 校验验证码（[VerifyOtpRequest]）
///       → ★ 用 vcode_token 登录（本请求）★ → 获得 access_token
///
/// ## 数据流位置
///
/// ```
/// AuthRepositoryImpl.login(countryCode, contact, vcodeToken)
///   → _client.executeRequest( ★ LoginRequest ★ )         ← 你在这里
///     → 服务端 POST /app/api/auth/login-user
///     → SDK 拆包 {code, message, data} envelope
///       → ★ LoginResponse ★                              ← 也在这里
///         → LoginResponse.toEntity() → User
/// ```

// ─────────────────────────────────────────────
//  Response DTO
// ─────────────────────────────────────────────

/// 登录响应中的用户档案，嵌套在 [LoginResponse.profile] 中。
///
/// 纯 Dart 类，无需任何注解。`_$LoginProfileFromJson` 由生成器从 `@ApiRequest` 声明中自动推导生成。
class LoginProfile {
  final int uid;
  final String uuid;
  @JsonKey(name: 'last_online')
  final int lastOnline;
  @JsonKey(name: 'profile_pic')
  final String profilePic;
  @JsonKey(name: 'profile_pic_gaussian')
  final String profilePicGaussian;
  final String nickname;
  final String contact;
  @JsonKey(name: 'country_code')
  final String countryCode;
  final String email;
  @JsonKey(name: 'recovery_email')
  final String recoveryEmail;
  final String username;
  final String bio;
  final int relationship;
  @JsonKey(name: 'user_alias')
  final String? userAlias;
  @JsonKey(name: 'channel_id')
  final int channelId;
  @JsonKey(name: 'channel_group_id')
  final int channelGroupId;
  final String hint;

  const LoginProfile({
    required this.uid,
    required this.uuid,
    required this.lastOnline,
    required this.profilePic,
    required this.profilePicGaussian,
    required this.nickname,
    required this.contact,
    required this.countryCode,
    required this.email,
    required this.recoveryEmail,
    required this.username,
    required this.bio,
    required this.relationship,
    this.userAlias,
    required this.channelId,
    required this.channelGroupId,
    required this.hint,
  });

  User toEntity() => User(
    uid: uid,
    uuid: uuid,
    lastOnline: lastOnline,
    profilePic: profilePic,
    profilePicGaussian: profilePicGaussian,
    nickname: nickname,
    contact: contact,
    countryCode: countryCode,
    email: email,
    recoveryEmail: recoveryEmail,
    username: username,
    bio: bio,
    relationship: relationship,
    userAlias: userAlias,
    hint: hint,
  );
}

/// 登录接口的业务响应数据（对应服务端 `data` 字段，即 T in `APIResponseWrapper<T>`）。
///
/// `{ code, message }` 由 SDK 内部的 `ApiResponseWrapper` 统一处理，
/// App 层只接触此类，不感知 envelope 结构。纯 Dart 类，无需任何注解。
class LoginResponse {
  @JsonKey(name: 'account_id')
  final String accountId;
  final LoginProfile profile;
  @JsonKey(name: 'access_token')
  final String accessToken;
  @JsonKey(name: 'refresh_token')
  final String refreshToken;
  @JsonKey(name: 'device_id')
  final String deviceId;
  final String nonce;
  @JsonKey(name: 'login_data')
  final String loginData;
  @JsonKey(name: 'is_verified')
  final bool? isVerified;

  const LoginResponse({
    required this.accountId,
    required this.profile,
    required this.accessToken,
    required this.refreshToken,
    required this.deviceId,
    this.nonce = '',
    this.loginData = '',
    this.isVerified,
  });

  User toEntity() => profile.toEntity();
}

// ─────────────────────────────────────────────
//  Request
// ─────────────────────────────────────────────

/// 使用 vcode_token 完成登录的请求
///
/// 上游：[VerifyOtpRequest] 返回的 `token` 即 vcodeToken。
/// 成功后 [LoginResponse.accessToken] 写入 ApiConfig，后续请求自动携带。
@ApiRequest(
  path: ApiPaths.authLogin,
  method: HttpMethod.post,
  responseType: LoginResponse,
  requestType: ApiRequestType.login,
)
class LoginRequest extends ApiRequestable<LoginResponse>
    with _$LoginRequestApi {
  @JsonKey(name: 'country_code')
  final String countryCode;
  final String contact;
  @JsonKey(name: 'vcode_token')
  final String vcodeToken;

  LoginRequest({
    required this.countryCode,
    required this.contact,
    required this.vcodeToken,
  });
}
