part of '../table_dtos.dart';

@freezed
class ListTableDto with _$ListTableDto {
  const ListTableDto._();

  const factory ListTableDto({
    @JsonKey(name: "tables") List<TableDto>? tables,
    @JsonKey(name: "total_count") int? totalCount,
    @JsonKey(name: "page") int? page,
    @JsonKey(name: "limit") int? limit,
    @JsonKey(name: "total_pages") int? totalPages,
  }) = _ListTableDto;

  factory ListTableDto.fromJson(Map<String, dynamic> json) =>
      _$ListTableDtoFromJson(json);

  ListTable toDomain() => ListTable(
    tables: tables?.map((dto) => dto.toDomain()).toList() ?? [],
    totalCount: totalCount ?? 0,
    page: page ?? 0,
    limit: limit ?? 0,
    totalPages: totalPages ?? 0,
  );
}

@freezed
class TableDto with _$TableDto {
  const TableDto._();

  const factory TableDto({
    @JsonKey(name: "id") String? id,
    @JsonKey(name: "organization_id") String? organizationId,
    @JsonKey(name: "outlet_id") String? outletId,
    @JsonKey(name: "table_name") String? tableName,
    @JsonKey(name: "status") String? status,
    @JsonKey(name: "payment_amount") int? paymentAmount,
    @JsonKey(name: "position_x") double? positionX,
    @JsonKey(name: "position_y") double? positionY,
    @JsonKey(name: "capacity") int? capacity,
    @JsonKey(name: "is_active") bool? isActive,
    @JsonKey(name: "created_at") String? createdAt,
    @JsonKey(name: "updated_at") String? updatedAt,
  }) = _TableDto;

  factory TableDto.fromJson(Map<String, dynamic> json) =>
      _$TableDtoFromJson(json);

  /// Mapping ke domain
  Table toDomain() => Table(
    id: id ?? '',
    organizationId: organizationId ?? '',
    outletId: outletId ?? '',
    tableName: tableName ?? '',
    status: status?.toTableStatusType() ?? TableStatusType.unknown,
    paymentAmount: paymentAmount ?? 0,
    positionX: positionX ?? 0.0,
    positionY: positionY ?? 0.0,
    capacity: capacity ?? 0,
    isActive: isActive ?? false,
    createdAt: createdAt != null ? DateTime.parse(createdAt!) : DateTime(1970),
    updatedAt: updatedAt != null ? DateTime.parse(updatedAt!) : DateTime(1970),
  );
}
