import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:superport/data/models/zipcode_dto.dart';

part 'company_dto.freezed.dart';
part 'company_dto.g.dart';

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

  const factory CompanyDto({
    int? id,
    required String name,
    @JsonKey(name: 'contact_name') required String contactName,
    @JsonKey(name: 'contact_phone') required String contactPhone,
    @JsonKey(name: 'contact_email') required String contactEmail,
    @JsonKey(name: 'parent_company_id') int? parentCompanyId,
    @JsonKey(name: 'zipcodes_zipcode') String? zipcodesZipcode,
    required String address,
    String? remark,
    @JsonKey(name: 'is_partner') @Default(false) bool isPartner,
    @JsonKey(name: 'is_customer') @Default(false) bool isCustomer,
    @JsonKey(name: 'is_active') @Default(false) bool isActive,
    @JsonKey(name: 'is_deleted') @Default(false) bool isDeleted,
    @JsonKey(name: 'registered_at') DateTime? registeredAt,
    @JsonKey(name: 'updated_at') DateTime? updatedAt,
    
    // Nested data (optional, populated in GET requests)
    @JsonKey(name: 'parent_company') CompanyNameDto? parentCompany,
    @JsonKey(name: 'zipcode') ZipcodeDto? zipcode,
  }) = _CompanyDto;

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

@freezed
class CompanyNameDto with _$CompanyNameDto {
  const factory CompanyNameDto({
    required int id,
    required String name,
  }) = _CompanyNameDto;

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

@freezed
class CompanyRequestDto with _$CompanyRequestDto {
  const factory CompanyRequestDto({
    required String name,
    @JsonKey(name: 'contact_name') required String contactName,
    @JsonKey(name: 'contact_phone') required String contactPhone,
    @JsonKey(name: 'contact_email') required String contactEmail,
    @JsonKey(name: 'parent_company_id') int? parentCompanyId,
    @JsonKey(name: 'zipcodes_zipcode') String? zipcodesZipcode,
    required String address,
    String? remark,
    @JsonKey(name: 'is_partner') @Default(false) bool isPartner,
    @JsonKey(name: 'is_customer') @Default(false) bool isCustomer,
    @JsonKey(name: 'is_active') @Default(false) bool isActive,
  }) = _CompanyRequestDto;

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

@freezed
class CompanyUpdateRequestDto with _$CompanyUpdateRequestDto {
  const factory CompanyUpdateRequestDto({
    String? name,
    @JsonKey(name: 'contact_name') String? contactName,
    @JsonKey(name: 'contact_phone') String? contactPhone,
    @JsonKey(name: 'contact_email') String? contactEmail,
    @JsonKey(name: 'parent_company_id') int? parentCompanyId,
    @JsonKey(name: 'zipcodes_zipcode') String? zipcodesZipcode,
    String? address,
    String? remark,
    @JsonKey(name: 'is_partner') bool? isPartner,
    @JsonKey(name: 'is_customer') bool? isCustomer,
    @JsonKey(name: 'is_active') bool? isActive,
  }) = _CompanyUpdateRequestDto;

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

@freezed
class CompanyListResponse with _$CompanyListResponse {
  const factory CompanyListResponse({
    @JsonKey(name: 'data') required List<CompanyDto> 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,
  }) = _CompanyListResponse;

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