export interface MarketPulsePoint {
  snapshot_date: string;
  rep_year: number;
  rep_month: number;
  total_square_th_sqm: number | null;
  sold_perc: number | null;
  price_avg: number | null;
}

export interface QuartirographyPortfolioRow {
  bucket: string;
  flat_count: number;
  area_sqm: number | null;
  percent: number | null;
  avg_area: number | null;
}

export interface QuartirographyDealsRow {
  bucket: string;
  deals: number;
  percent: number;
  median_price: number | null;
}

export interface PipelineRow {
  year: string;
  total_th_sqm: number | null;
  sold_perc: number | null;
  unsold_perc: number | null;
  unopened_perc: number | null;
}

export interface DistrictRow {
  district_name: string;
  zk_count: number | null;
  flat_count: number | null;
  area_m2: number | null;
  median_price_per_m2: number | null;
  mean_price_per_m2: number | null;
  cad_quarter_count: number;
}

export interface YandexListing {
  yid: number;
  name: string;
  developer: string | null;
  obj_class: string | null;
  flats_total: number;
  price_from: number | null;
  price_to: number | null;
  address: string | null;
  lat: number | null;
  lon: number | null;
}

export interface YandexListingsSummary {
  snapshot_date: string | null;
  total: number;
  by_class: { obj_class: string; count: number }[];
  items: YandexListing[];
}

export interface DeveloperTopRow {
  developer_id: string;
  developer_name: string;
  jk_count: number | null;
  jk_flats_total: number | null;
  sverdl_sqm_th: number | null;
  sold_pct: number | null;
  sold_delta_pp: number | null;
  sold_first: number | null;
  sold_last: number | null;
  first_dt: string | null;
  last_dt: string | null;
  avg_area_sqm: number | null;
  pct_one: number | null;
  pct_three_plus: number | null;
}

export interface DeveloperDetail {
  developer_id: string;
  developer_name: string;
  jk_count: number | null;
  jk_flats_total: number | null;
  jk_sqm_total: number | null;
  jk_ekb: number | null;
  jk_completed: number | null;
  jk_in_progress: number | null;
  jk_escrow: number | null;
  agg_flats_total: number | null;
  agg_one_room: number | null;
  agg_two_room: number | null;
  agg_three_room: number | null;
  agg_four_plus: number | null;
  pct_one: number | null;
  pct_three_plus: number | null;
  avg_area_sqm: number | null;
  sverdl_sqm_th: number | null;
  sverdl_sold_pct: number | null;
  sverdl_unsold_pct: number | null;
  sverdl_price_avg: number | null;
}

export interface DeveloperHistoryPoint {
  developer_id: string;
  snapshot_date: string;
  sold_perc: number | null;
  total_th_sqm: number | null;
}

export interface DeveloperPortfolioObject {
  obj_id: number;
  comm_name: string | null;
  addr: string | null;
  region_cd: number | null;
  flat_count: number | null;
  square_living: number | null;
  ready_dt: string | null;
  obj_class: string | null;
  escrow: boolean | null;
  problem_flag: string | null;
  lat: number | null;
  lon: number | null;
  is_ekb: boolean | null;
}

export interface PrinzipDistrictRow {
  district_name: string;
  prinzip_zk: number;
  share_in_district_pct: number;
}

export interface ObjectDetail {
  obj_id: number;
  hobj_id: number | null;
  comm_name: string | null;
  addr: string | null;
  short_addr: string | null;
  region_cd: number | null;
  dev_id: string | null;
  dev_name: string | null;
  floor_min: number | null;
  floor_max: number | null;
  flat_count: number | null;
  square_living: number | null;
  ready_dt: string | null;
  site_status: string | null;
  escrow: boolean | null;
  obj_class: string | null;
  latitude: number | null;
  longitude: number | null;
  obj_status: number | null;
  snapshot_date: string | null;
  buildings_count: number | null;
}

export interface ObjectBuilding {
  cad_num: string | null;
  floors: number | null;
  area: number | null;
  purpose: string | null;
  building_name: string | null;
  readable_address: string | null;
  geom_geojson: unknown;
}

export interface ObjectSaleGraphPoint {
  report_month: string | null;
  type: string;
  realised: number | null;
  contracted: number | null;
  area_sq: number | null;
  price_avg: number | null;
}

export interface ObjectSalesAggRow {
  type: string;
  name: string | null;
  total: number | null;
  realised: number | null;
  perc: number | null;
}

export interface ObjectInfraPoi {
  poi_name: string;
  poi_subtitle: string | null;
  poi_category: string | null;
  poi_address: string | null;
  lat: number | null;
  lon: number | null;
  distance_m: number | null;
}

export interface MetroEntry {
  name: string;
  time: number | null;
  line: string | null;
  color: string | null;
  isWalk: boolean | null;
}

export interface ObjectFullDetail {
  obj_id: number;
  hobj_id: number | null;
  comm_name: string | null;
  addr: string | null;
  short_addr: string | null;
  region_cd: number | null;
  dev_id: string | null;
  dev_name: string | null;
  dev_group_name: string | null;
  floor_min: number | null;
  floor_max: number | null;
  flat_count: number | null;
  square_living: number | null;
  ready_dt: string | null;
  site_status: string | null;
  escrow: boolean | null;
  obj_class: string | null;
  latitude: number | null;
  longitude: number | null;
  obj_status: number | null;
  snapshot_date: string | null;
  energy_eff: string | null;
  wall_type: string | null;
  // Building specs
  first_floor_type: string | null;
  section_count: number | null;
  elevators_passenger_count: number | null;
  elevators_cargo_count: number | null;
  parking_total_slots: number | null;
  guest_parking_inside_count: number | null;
  guest_parking_outside_count: number | null;
  ceiling_height_m: number | null;
  // Apartment summary
  finishing_variants_count: number | null;
  has_free_planning: boolean | null;
  avg_flat_area_m2: number | null;
  // Yard
  playground_kids_count: number | null;
  playground_sport_count: number | null;
  has_bike_paths: boolean | null;
  trash_areas_count: number | null;
  // OVZ
  has_ramp: boolean | null;
  has_low_platforms: boolean | null;
  has_wheelchair_lift: boolean | null;
  // Catalog/UI
  flat_area_min: number | null;
  flat_area_max: number | null;
  price_min_rub: number | null;
  price_max_rub: number | null;
  price_per_m2_min: number | null;
  price_per_m2_max: number | null;
  parking_provision_pct: number | null;
  project_published_at: string | null;
  project_declaration_num: string | null;
  // Metro & scores
  metro_nearest_name: string | null;
  metro_nearest_walk_minutes: number | null;
  metro_top3: MetroEntry[] | null;
  domrf_score_location: number | null;
  domrf_score_transport: number | null;
  domrf_score_infrastructure: number | null;
  buildings_count: number;
}

export interface ObjectQuartirographyRow {
  room_label: string;
  total_count: number;
  free_count: number;
  area_min: number | null;
  area_max: number | null;
  price_min: number | null;
  price_max: number | null;
}

export interface ObjectCheckRow {
  check_type: string;
  passed: boolean;
  checked_at: string | null;
}

export interface ObjectDocumentRow {
  doc_type: string;
  doc_num: string | null;
  posted_at: string | null;
  file_url: string;
  size_bytes: number | null;
}

export interface ObjectPhoto {
  obj_file_id: string;
  ord_num: number | null;
  photo_url: string | null;
  thumb_url: string;
  full_url: string;
  photo_dttm: string | null;
  period_dt: string | null;
  size_bytes: number | null;
  photo_name: string | null;
  ready_desc: string | null;
  build_type: string | null;
  local_path: string | null;
}

// ── Recommender (Уровень 1) ─────────────────────────────────────────────────

export type RecommendClass = "Comfort" | "Comfort+" | "Business" | "Elite";

export interface RecommendMixInput {
  district_name: string;
  area_total_m2: number | null;
  target_class: RecommendClass | null;
  months_window: number;
  price_factor?: number;
  target_months?: number | null;
}

export interface RecommendBucket {
  bucket: string;
  share_pct: number;
  deal_count: number;
  area_avg_m2: number;
  area_median_m2: number;
  price_median_per_m2: number;
  price_p25_per_m2: number;
  price_p75_per_m2: number;
  units_planned: number | null;
  revenue_planned_rub: number | null;
  velocity_per_month: number | null;
  months_to_sellout: number | null;
  elasticity?: number;
  elasticity_r2?: number;
  elasticity_n?: number;
  elasticity_source?: "regression" | "fallback_global";
  /** #25 — top success bucket flag */
  is_top_success?: boolean;
}

export interface ElasticityPerBucket {
  [bucketPretty: string]: {
    elasticity: number;
    r2: number;
    n: number;
    source: "regression" | "fallback_global";
  };
}

export interface RecommendComparable {
  obj_id: number;
  comm_name: string | null;
  dev_name: string | null;
  obj_class: string | null;
  flat_count: number | null;
  sold_perc: number | null;
  cad_quarter: string | null;
  lat: number | null;
  lon: number | null;
  buildings_n: number | null;
}

export interface RecommendMixOutput {
  scope: {
    district: string;
    district_zk_count: number | null;
    district_median_price_per_m2: number | null;
    district_factor: number;
    class_multiplier: number;
    target_class: string | null;
    class_multiplier_source: "realty_zk_vs_comfort" | null;
    months_window: number;
    effective_window_months: number;
    region_code: number;
    total_deals: number;
    market_velocity_per_month: number | null;
    velocity_source: "objective" | "sale_graph" | "rosreestr_fallback";
    velocity_observations: number;
    velocity_objects: number;
    competitors_count: number;
    competitors_scope:
      | "district+class"
      | "district"
      | "region"
      | "fallback_singleton";
    saturation_median: number | null;
    saturation_n: number;
    sat_factor: number;
    velocity_trend_ratio: number | null;
    trend_recent_units: number;
    trend_prior_units: number;
    trend_factor: number;
    poi_score: number | null;
    poi_score_city_avg: number | null;
    poi_factor: number;
    mortgage_rate_pct: number | null;
    mortgage_rate_period: string | null;
    /** #237 — дата актуальности данных (ISO 'YYYY-MM-DD') | null */
    last_updated: string | null;
    elasticity: number;
    elasticity_r2: number;
    elasticity_n: number;
    elasticity_source: "regression" | "fallback";
    elasticity_weighted: number | null;
    elasticity_per_bucket: ElasticityPerBucket;
    share_window_months: number;
    elasticity_window_months: number;
    /** #22 — noise penalty (0.9..1.0) */
    noise_penalty?: number;
    noise_breakdown?:
      | {
          district: string;
          magistral_n: number;
          railway_n: number;
          industrial_n: number;
          total_sources: number;
          penalty_pct: number;
        }
      | Record<string, never>;
    /** #23 — 2D competitors */
    competitors_radius_n?: number;
    competitors_district_only_n?: number;
    /** #25 — success ranking */
    success_ranking?: Array<{
      bucket: string;
      success_score: number;
      n_deals: number;
      velocity_z: number;
      price_z: number;
      area_z: number;
    }>;
    cadastre_median_per_m2: number | null;
    cadastre_buildings_n: number;
    cadastre_vs_market_pct: number | null;
    price_factor_applied: number;
    required_price_factor: number | null;
    target_months: number | null;
    data_caveat?: string;
    error?: string;
  };
  buckets: RecommendBucket[];
  summary: {
    total_revenue_rub: number | null;
    weighted_avg_price_per_m2: number | null;
    total_units_planned: number | null;
    months_to_sellout_total: number | null;
    avg_ticket_rub: number | null;
    liquidity_score_24mo: number | null;
    headline: string | null;
    warnings: string[];
  };
  comparables: RecommendComparable[];
}

export interface PrinzipFunnelMonthRow {
  month: string | null;
  source: string;
  leads: number;
  engaged: number;
  converted: number;
  conv_pct: number | null;
}

export interface PrinzipFunnelSourceRow {
  source: string;
  leads: number;
  engaged: number;
  converted: number;
  conv_pct: number | null;
}

export interface PrinzipFunnelObjectRow {
  obj_id: number;
  comm_name: string | null;
  leads_count: number;
  deals_count: number;
  conv_pct: number | null;
  total_revenue: number | null;
  avg_deal_price: number | null;
}

export interface PrinzipObjectRow {
  obj_id: number;
  comm_name: string | null;
  addr: string | null;
  flat_count: number | null;
  square_living: number | null;
  ready_dt: string | null;
  site_status: string | null;
  total: number | null;
  realised: number | null;
  perc: number | null;
  sparkline_realised: number[];
  months: string[];
}

export interface PrinzipInsights {
  headline: string;
  key_gaps: {
    label: string;
    prinzip: number;
    market: number;
    brusnika: number;
    forum: number;
    unit: string;
  }[];
  priorities: { rank: number; title: string; why: string }[];
  where_to_build: { district: string; why: string }[];
  what_to_avoid: string[];
  benchmarks: { name: string; model: string }[];
}
