// ─── Search Config ──────────────────────────────────────────────

export interface SearchConfig {
  location: string;
  propertyTypes?: string[];
  building?: {
    yearBuiltFrom?: number;
    yearBuiltUntil?: number;
    yearRenovatedFrom?: number;
    yearRenovatedUntil?: number;
    zoning?: string;
    lotSizeSfMin?: number;
    lotSizeSfMax?: number;
    lotSizeAcresMin?: number;
    lotSizeAcresMax?: number;
    opportunityZone?: boolean;
    totalUnitsMin?: number;
    totalUnitsMax?: number;
    buildingAreaMin?: number;
    buildingAreaMax?: number;
  };
  owner?: {
    nameOrCompany?: string;
    ownerType?: "Company" | "Person";
    includesPhone?: boolean;
    includesEmail?: boolean;
    includesMailingAddress?: boolean;
    portfolioMin?: number;
    portfolioMax?: number;
    ownerOccupied?: boolean;
    inStateOwner?: boolean;
    portfolioValueMin?: number;
    portfolioValueMax?: number;
    reportedOwner?: string;
    mailingAddress?: string;
  };
  occupants?: {
    name?: string;
    naicsSic?: string;
    website?: string;
  };
  sales?: {
    dateRange?: string;
    multiParcel?: boolean;
    priceMin?: number;
    priceMax?: number;
    pricePerSfMin?: number;
    pricePerSfMax?: number;
    likelyToSell?: boolean;
  };
  debt?: {
    amountMin?: number;
    amountMax?: number;
    originationFrom?: string;
    originationUntil?: string;
    maturityFrom?: string;
    maturityUntil?: string;
    lenderName?: string;
    cmbsLoan?: boolean;
  };
  distressed?: {
    auctionDateFrom?: string;
    auctionDateUntil?: string;
    preForeclosureCategory?: string;
    cmbsWatchlist?: boolean;
  };
}

// ─── Output Config ──────────────────────────────────────────────

export interface OutputConfig {
  propertyInfo?: ("address" | "type" | "units" | "sqft" | "yearBuilt" | "lotSize" | "zoning" | "opportunityZone" | "apn" | "legal")[];
  ownerInfo?: ("name" | "company" | "portfolioSize" | "portfolioValue" | "ownerType")[];
  contactInfo?: ("phones" | "emails")[];
  salesInfo?: ("lastSaleDate" | "lastSalePrice" | "buyer" | "seller" | "deedType")[];
  debtInfo?: ("lender" | "loanType" | "mortgageAmount" | "maturityDate" | "interestType")[];
  taxInfo?: ("assessedValue" | "taxAmount")[];
}

// ─── Agent Browser Result ───────────────────────────────────────

export interface ABResult {
  success: boolean;
  output?: string;
  error?: string;
}

// ─── Contact Types ──────────────────────────────────────────────

export interface Phone {
  number: string;
  type: string;
}

export interface Owner {
  name: string;
  phones: Phone[];
  emails: string[];
}

// ─── Extractor Data Types ───────────────────────────────────────

export interface BuildingData {
  stories?: string;
  buildings?: string;
  units?: string;
  buildingArea?: string;
  propertyType?: string;
  lotSize?: string;
  zoning?: string;
  yearBuilt?: string;
  opportunityZone?: string;
  msa?: string;
  county?: string;
  legal?: string;
}

export interface SalesRecord {
  buyer?: string;
  seller?: string;
  saleDate?: string;
  price?: string;
  deedType?: string;
}

export interface DebtRecord {
  lender?: string;
  loanType?: string;
  mortgageType?: string;
  borrower?: string;
  amount?: string;
  originationDate?: string;
  maturityDate?: string;
}

export interface TaxData {
  assessedValue?: string;
  taxAmount?: string;
  taxYear?: string;
}

// ─── Lead ───────────────────────────────────────────────────────

export interface Lead {
  id?: string;
  jobId: string;
  scrapeDate: string;
  propertyId: string;
  propertyAddress: string;
  building?: BuildingData;
  owners?: Owner[];
  sales?: SalesRecord[];
  debt?: DebtRecord[];
  tax?: TaxData;
}

// ─── Job Types ──────────────────────────────────────────────────

export type JobStatus = "pending" | "running" | "queued" | "searching" | "scraping" | "complete" | "completed" | "failed";

export interface JobProgress {
  total: number;
  processed: number;
  leads: number;
}

export interface Job {
  id: string;
  status: JobStatus;
  searchConfig: SearchConfig;
  outputConfig?: OutputConfig;
  searchId?: string;
  maxProperties: number;
  totalProperties?: number;
  scrapedCount?: number;
  results?: PropertyResult[];
  error?: string;
  createdAt: string;
  updatedAt?: string;
  startedAt?: string;
  completedAt?: string;
  progress?: JobProgress;
}

// ─── Scrape Request ─────────────────────────────────────────────

export interface ScrapeRequest {
  searchConfig: SearchConfig;
  outputConfig: OutputConfig;
  maxProperties?: number;
  searchId?: string;
}

// ─── Property Result (legacy flat format) ───────────────────────

export interface PropertyResult {
  address?: string;
  city?: string;
  state?: string;
  zip?: string;
  propertyType?: string;
  units?: number;
  sqft?: number;
  yearBuilt?: number;
  lotSize?: string;
  zoning?: string;
  opportunityZone?: boolean;
  apn?: string;
  ownerName?: string;
  ownerCompany?: string;
  ownerType?: string;
  portfolioSize?: number;
  portfolioValue?: number;
  phones?: string[];
  emails?: string[];
  lastSaleDate?: string;
  lastSalePrice?: number;
  buyer?: string;
  seller?: string;
  lender?: string;
  mortgageAmount?: number;
  maturityDate?: string;
  assessedValue?: number;
  taxAmount?: number;
  [key: string]: unknown;
}
