import { saleLabel } from './sale';

/** Sipariş kaydından, emlak portallarına yapıştırılacak ilan başlığı ve açıklaması üretir.
 *  Yalnızca kullanıcının verdiği + Tapu Kadastro verisi kullanılır; uydurma özellik eklenmez. */
export type ListingOrder = {
  il: string; ilce: string; mahalle: string; ada: string; parsel: string;
  areaM2?: number | null; nitelik?: string | null;
  saleType?: string | null; unitType?: string | null; unitArea?: number | null; rooms?: string | null; floorNo?: string | null; buildYear?: number | null; share?: string | null;
  imarDurum?: string | null; emsal?: string | null; taks?: string | null; gabari?: string | null;
  highlights?: string[]; headline?: string | null; price?: number | null;
};

const tl = (n: number) => n.toLocaleString('tr-TR');
const m2 = (n?: number | null) => (n ? `${tl(Math.round(n))} m²` : '');
const hasAda = (a?: string | null) => !!String(a ?? '').trim() && String(a).trim() !== '0';
const ap = (o: ListingOrder) => (hasAda(o.ada) ? `${o.ada} ada ${o.parsel} parsel` : `${o.parsel} parsel`);
const floor = (f?: string | null) => (f ? (/^-?\d+$/.test(f) ? `${f}. kat` : f) : '');
const built = (t?: string | null) => t === 'tamami' || t === 'yapi' || t === 'bagimsiz';

export function listingTitle(o: ListingOrder): string {
  if (o.headline?.trim()) return o.headline.trim();
  const where = `${o.mahalle}`;
  if (built(o.saleType)) {
    const u = o.unitType || 'Yapı';
    return [o.rooms, u, where ? `${where}'de` : '', 'satılık'].filter(Boolean).join(' ').replace(/\s+/g, ' ');
  }
  if (o.saleType === 'hisse') return `${where}'de satılık hisseli ${(o.nitelik || 'arsa').toLocaleLowerCase('tr-TR')}`;
  return `${where}'de ${m2(o.areaM2)} satılık ${(o.nitelik || 'arsa').toLocaleLowerCase('tr-TR')}`.replace(/\s+/g, ' ');
}

export function listingBody(o: ListingOrder): string {
  const L: string[] = [];
  const yer = `${o.il} ili, ${o.ilce} ilçesi, ${o.mahalle}`;
  if (built(o.saleType)) {
    const u = (o.unitType || 'yapı');
    const bits = [o.rooms, o.unitArea ? `brüt ${m2(o.unitArea)}` : '', floor(o.floorNo), o.buildYear ? `${o.buildYear} yapımı` : ''].filter(Boolean).join(', ');
    L.push(`${yer} bölgesinde, ${ap(o)} üzerinde satılık ${u.toLocaleLowerCase('tr-TR')}${bits ? `. ${bits.charAt(0).toLocaleUpperCase('tr-TR') + bits.slice(1)}.` : '.'}`);
    if (o.areaM2) L.push(`Taşınmazın bulunduğu parselin yüzölçümü ${m2(o.areaM2)}.`);
  } else if (o.saleType === 'hisse' && o.share) {
    L.push(`${yer} bölgesinde, ${ap(o)} kayıtlı taşınmazın ${o.share} hissesi satılıktır. Parselin toplam yüzölçümü ${m2(o.areaM2)}.`);
  } else {
    L.push(`${yer} bölgesinde, ${ap(o)} kayıtlı ${m2(o.areaM2)} ${(o.nitelik || 'arsa').toLocaleLowerCase('tr-TR')} satılıktır.`);
  }
  const imar = [o.imarDurum && `imar durumu ${o.imarDurum.toLocaleLowerCase('tr-TR')}`, o.emsal && `emsal ${o.emsal}`, o.taks && `TAKS ${o.taks}`, o.gabari && `gabari ${o.gabari}`].filter(Boolean);
  if (imar.length) L.push(`İmar bilgileri: ${imar.join(', ')}.`);
  const hl = (o.highlights || []).map((x) => x.trim()).filter(Boolean);
  if (hl.length) L.push(hl.map((x) => '• ' + x).join('\n'));
  L.push(`Parsel sınırı ve yüzölçümü Tapu Kadastro verisine dayanır; konumu gösteren tanıtım videosu ilanla birlikte paylaşılmaktadır.`);
  if (o.price) L.push(`Fiyat: ${tl(o.price)} TL.`);
  L.push(`Detaylı bilgi ve yerinde inceleme için iletişime geçebilirsiniz.`);
  return L.join('\n\n');
}

export const listingText = (o: ListingOrder) => ({ title: listingTitle(o), body: listingBody(o) });
