import { type ListingStatus, type TransactionType, type PropertyType, type Direction, type Furnishing, type PropertyCondition } from '@prisma/client';

/** AVM-based valuation estimate bundled into listing detail. Cached 24 h per listing. */
export interface ValuationEstimate {
  value: string;
  confidence: number;
  modelVersion: string;
  estimatedAt: string;
}

/** Agent quality score denormalised from the agent profile. */
export interface AgentQualityScore {
  score: number;
  tier: 'BRONZE' | 'SILVER' | 'GOLD' | 'PLATINUM';
}

/** Returned by findByIdWithProperty — full listing detail with property, seller, agent */
export interface ListingDetailData {
  id: string;
  status: ListingStatus;
  transactionType: TransactionType;
  priceVND: string;
  pricePerM2: number | null;
  rentPriceMonthly: string | null;
  commissionPct: number | null;
  viewCount: number;
  saveCount: number;
  /**
   * Total inquiry count on this listing.
   * Visible only to the listing owner or an admin; public callers receive `null`.
   */
  inquiryCount: number | null;
  isFeatured: boolean;
  featuredUntil: string | null;
  publishedAt: string | null;
  createdAt: string;
  /** AVM valuation estimate (cached 24 h). `null` when the AVM service is unavailable. */
  valuationEstimate: ValuationEstimate | null;
  /** Quality score of the assigned agent. `null` when no agent is assigned. */
  agentQualityScore: AgentQualityScore | null;
  /** Number of ACTIVE listings matching this one's type / district / price range. */
  similarCount: number;
  property: {
    id: string;
    propertyType: PropertyType;
    title: string;
    description: string;
    address: string;
    ward: string;
    district: string;
    city: string;
    latitude: number;
    longitude: number;
    areaM2: number;
    usableAreaM2: number | null;
    bedrooms: number | null;
    bathrooms: number | null;
    floors: number | null;
    floor: number | null;
    totalFloors: number | null;
    direction: Direction | null;
    yearBuilt: number | null;
    legalStatus: string | null;
    amenities: unknown;
    nearbyPOIs: unknown;
    metroDistanceM: number | null;
    projectName: string | null;
    furnishing: Furnishing | null;
    propertyCondition: PropertyCondition | null;
    balconyDirection: Direction | null;
    maintenanceFeeVND: string | null;
    parkingSlots: number | null;
    viewType: string[];
    petFriendly: boolean | null;
    suitableFor: string[];
    whyThisLocation: string | null;
    media: ListingMediaData[];
  };
  seller: {
    id: string;
    fullName: string;
    phone: string;
  };
  agent: {
    id: string;
    userId: string;
    agency: string | null;
  } | null;
}

export interface ListingMediaData {
  id: string;
  url: string;
  type: string;
  order: number;
  caption: string | null;
}

/** Returned by search / findByStatus — listing summary with thumbnail */
export interface ListingSearchItem {
  id: string;
  status: ListingStatus;
  transactionType: TransactionType;
  priceVND: string;
  pricePerM2: number | null;
  isFeatured: boolean;
  featuredUntil: string | null;
  viewCount: number;
  publishedAt: string | null;
  property: {
    id: string;
    propertyType: PropertyType;
    title: string;
    address: string;
    district: string;
    city: string;
    latitude: number;
    longitude: number;
    areaM2: number;
    bedrooms: number | null;
    bathrooms: number | null;
    thumbnail: string | null;
    media: ListingMediaData[];
  };
  seller: {
    id: string;
    fullName: string;
  };
}

/** Returned by findSimilar — compact comparable listing for the "similar listings" widget */
export interface ListingSimilarItem {
  id: string;
  title: string;
  priceVND: string;
  areaM2: number;
  district: string;
  thumbnailUrl: string | null;
  publishedAt: string | null;
}

/** Returned by findBySellerId — compact listing for seller dashboard */
export interface ListingSellerItem {
  id: string;
  status: ListingStatus;
  transactionType: TransactionType;
  priceVND: string;
  isFeatured: boolean;
  featuredUntil: string | null;
  property: {
    id: string;
    title: string;
    district: string;
    city: string;
    areaM2: number;
    thumbnail: string | null;
    media: ListingMediaData[];
  };
}
