import type { TransferCategory, TransferCondition, TransferListingStatus, TransferPricingSource } from '@prisma/client';
import type { CreateTransferItemInput } from '../../application/commands/create-transfer-listing/create-transfer-listing.command';
import type { TransferListingEntity } from '../entities/transfer-listing.entity';

export const TRANSFER_LISTING_REPOSITORY = Symbol('TRANSFER_LISTING_REPOSITORY');

export interface TransferListingSearchParams {
  query?: string;
  category?: TransferCategory;
  status?: TransferListingStatus;
  district?: string;
  city?: string;
  minPrice?: number;
  maxPrice?: number;
  sellerId?: string;
  page?: number;
  limit?: number;
}

export interface PaginatedResult<T> {
  data: T[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}

export interface TransferListingListItem {
  id: string;
  sellerId: string;
  category: TransferCategory;
  status: TransferListingStatus;
  title: string;
  address: string;
  district: string;
  city: string;
  latitude: number;
  longitude: number;
  askingPriceVND: bigint;
  aiEstimatePriceVND: bigint | null;
  pricingSource: TransferPricingSource;
  isNegotiable: boolean;
  areaM2: number | null;
  media: Record<string, unknown>[] | null;
  viewCount: number;
  inquiryCount: number;
  publishedAt: Date | null;
  itemCount: number;
}

export interface TransferItemData {
  id: string;
  name: string;
  brand: string | null;
  modelName: string | null;
  category: TransferCategory;
  condition: TransferCondition;
  purchaseYear: number | null;
  originalPriceVND: bigint | null;
  askingPriceVND: bigint;
  aiEstimatePriceVND: bigint | null;
  aiConfidence: number | null;
  quantity: number;
  dimensions: Record<string, unknown> | null;
  media: Record<string, unknown>[] | null;
  notes: string | null;
}

export interface TransferListingDetailData {
  id: string;
  sellerId: string;
  category: TransferCategory;
  status: TransferListingStatus;
  title: string;
  description: string | null;
  address: string;
  ward: string | null;
  district: string;
  city: string;
  latitude: number;
  longitude: number;
  askingPriceVND: bigint;
  aiEstimatePriceVND: bigint | null;
  aiConfidence: number | null;
  pricingSource: TransferPricingSource;
  isNegotiable: boolean;
  areaM2: number | null;
  monthlyRentVND: bigint | null;
  depositMonths: number | null;
  remainingLeaseMo: number | null;
  businessType: string | null;
  footTraffic: string | null;
  media: Record<string, unknown>[] | null;
  moderationScore: number | null;
  moderationNotes: string | null;
  viewCount: number;
  saveCount: number;
  inquiryCount: number;
  contactPhone: string | null;
  contactName: string | null;
  featuredUntil: Date | null;
  expiresAt: Date | null;
  publishedAt: Date | null;
  items: TransferItemData[];
  createdAt: Date;
  updatedAt: Date;
}

export interface TransferStatsData {
  totalListings: number;
  totalValue: bigint;
  byCategory: { category: string; count: number; avgPrice: number }[];
  byDistrict: { district: string; count: number; avgPrice: number }[];
  byStatus: { status: string; count: number }[];
}

export interface ITransferListingRepository {
  findById(id: string): Promise<TransferListingEntity | null>;
  findDetailById(id: string): Promise<TransferListingDetailData | null>;
  save(entity: TransferListingEntity, items: CreateTransferItemInput[]): Promise<void>;
  update(entity: TransferListingEntity): Promise<void>;
  search(params: TransferListingSearchParams): Promise<PaginatedResult<TransferListingListItem>>;
  getStats(): Promise<TransferStatsData>;
}
