import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:superport/data/models/company/company_dto.dart';
import 'package:superport/models/user_model.dart';

part 'user_dto.freezed.dart';
part 'user_dto.g.dart';

@freezed
class UserDto with _$UserDto {
  const UserDto._(); // Private constructor for getters

  const factory UserDto({
    int? id,
    required String name,
    String? phone,
    String? email,
    @JsonKey(name: 'companies_id') required int companiesId,
    
    // Nested data (optional, populated in GET requests)
    @JsonKey(name: 'company') CompanyNameDto? company,
  }) = _UserDto;

  factory UserDto.fromJson(Map<String, dynamic> json) => _$UserDtoFromJson(json);
  
  // 도메인 모델로 변환
  User toDomainModel() {
    return User(
      id: id,
      name: name,
      email: email,
      phone: phone,
      companyName: company?.name,
    );
  }
}

@freezed
class UserRequestDto with _$UserRequestDto {
  const factory UserRequestDto({
    required String name,
    String? phone,
    String? email,
    @JsonKey(name: 'companies_id') required int companiesId,
  }) = _UserRequestDto;

  factory UserRequestDto.fromJson(Map<String, dynamic> json) =>
      _$UserRequestDtoFromJson(json);
}

@freezed
class UserUpdateRequestDto with _$UserUpdateRequestDto {
  const factory UserUpdateRequestDto({
    String? name,
    String? phone,
    String? email,
    @JsonKey(name: 'companies_id') int? companiesId,
  }) = _UserUpdateRequestDto;

  factory UserUpdateRequestDto.fromJson(Map<String, dynamic> json) =>
      _$UserUpdateRequestDtoFromJson(json);
      
  // 도메인 모델에서 생성
  factory UserUpdateRequestDto.fromDomain(User user, {String? newPassword}) {
    return UserUpdateRequestDto(
      name: user.name,
      phone: user.phone,
      email: user.email,
      companiesId: null, // companiesId는 업데이트에서 처리하지 않음
    );
  }
}

@freezed
class UserListResponse with _$UserListResponse {
  const factory UserListResponse({
    @JsonKey(name: 'data') required List<UserDto> items,
    @JsonKey(name: 'total') required int totalCount,
    @JsonKey(name: 'page') required int currentPage,
    @JsonKey(name: 'total_pages') required int totalPages,
    @JsonKey(name: 'page_size') int? pageSize,
  }) = _UserListResponse;

  factory UserListResponse.fromJson(Map<String, dynamic> json) => 
      _$UserListResponseFromJson(json);
}

// UserListDto (legacy support)
@freezed
class UserListDto with _$UserListDto {
  const UserListDto._(); // Private constructor for methods

  const factory UserListDto({
    @JsonKey(name: 'users') required List<UserDto> users,
    @JsonKey(name: 'total') required int total,
    @JsonKey(name: 'page') required int page,
    @JsonKey(name: 'per_page') required int perPage,
    @JsonKey(name: 'total_pages') required int totalPages,
  }) = _UserListDto;

  factory UserListDto.fromJson(Map<String, dynamic> json) =>
      _$UserListDtoFromJson(json);
      
  // 도메인 모델 리스트로 변환
  List<User> toDomainModels() {
    return users.map((dto) => dto.toDomainModel()).toList();
  }
  
  // 페이지네이션 정보 접근을 위한 getter들
  int get first => page;
  int get last => totalPages;
}

// CheckUsernameResponse (legacy support)
@freezed
class CheckUsernameResponse with _$CheckUsernameResponse {
  const factory CheckUsernameResponse({
    @JsonKey(name: 'available') required bool available,
    String? message,
  }) = _CheckUsernameResponse;

  factory CheckUsernameResponse.fromJson(Map<String, dynamic> json) =>
      _$CheckUsernameResponseFromJson(json);
}
