import type { Tag, TagCategory } from "@/core/tagging/types";
import type { CompanyId } from "@/core/auth/types";

type SeedTag = Omit<Tag, "id" | "createdAt">;

/** Parse project line like "000 Farmacie" → { code: "B-000", label: "Farmacie" } */
function parseProjectLine(
  line: string,
  prefix: string,
): { code: string; label: string } | null {
  const match = line.match(/^(\w?\d+)\s+(.+)$/);
  if (!match?.[1] || !match[2]) return null;
  const num = match[1];
  const label = match[2].trim();
  const padded = num.replace(/^[A-Z]/, "").padStart(3, "0");
  const codePrefix = num.startsWith("L") ? `${prefix}L` : prefix;
  return { code: `${codePrefix}-${padded}`, label };
}

export function getManicTimeSeedTags(): SeedTag[] {
  const tags: SeedTag[] = [];

  // ── Beletage projects ──
  const beletageProjects = [
    "000 Farmacie",
    "002 Cladire birouri Stratec",
    "003 PUZ Bellavista",
    "007 Design Apartament Teodora",
    "010 Casa Doinei",
    "016 Duplex Eremia",
    "024 Bloc Petofi",
    "028 PUZ Borhanci-Sopor",
    "033 Mansardare Branului",
    "039 Cabinete Stoma Scala",
    "041 Imobil mixt Progresului",
    "045 Casa Andrei Muresanu",
    "052 PUZ Carpenului",
    "059 PUZ Nordului",
    "064 Casa Salicea",
    "066 Terasa Gherase",
    "070 Bloc Fanatelor",
    "073 Case Frumoasa",
    "074 PUG Cosbuc",
    "076 Casa Copernicus",
    "077 PUZ Schimbare destinatie Brancusi",
    "078 Service auto Linistei",
    "079 Amenajare drum Servitute Eremia",
    "080 Bloc Tribunul",
    "081 Extindere casa Gherase",
    "083 Modificari casa Zsigmund 18",
    "084 Mansardare Petofi 21",
    "085 Container CT Spital Tabacarilor",
    "086 Imprejmuire casa sat Gheorgheni",
    "087 Duplex Oasului fn",
    "089 PUZ A-Liu Sopor",
    "090 VR MedEvents",
    "091 Reclama Caparol",
    "092 Imobil birouri 13 Septembrie",
    "093 Casa Salistea Noua",
    "094 PUD Casa Rediu",
    "095 Duplex Vanatorului",
    "096 Design apartament Sopor",
    "097 Cabana Gilau",
    "101 PUZ Gilau",
    "102 PUZ Ghimbav",
    "103 Piscine Lunca Noua",
    "104 PUZ REGHIN",
    "105 CUT&Crust",
    "106 PUZ Mihai Romanu Nord",
    "108 Reabilitare Bloc Beiusului",
    "109 Case Samboleni",
    "110 Penny Crasna",
    "111 Anexa Piscina Borhanci",
    "112 PUZ Blocuri Bistrita",
    "113 PUZ VARATEC-FIRIZA",
    "114 PUG Husi",
    "115 PUG Josenii Bargaului",
    "116 PUG Monor",
    "117 Schimbare Destinatie Mihai Viteazu 2",
    "120 Anexa Brasov",
    "121 Imprejurare imobil Mesterul Manole 9",
    "122 Fastfood Bashar",
    "123 PUD Rediu 2",
    "127 Casa Socaciu Ciurila",
    "128 Schimbare de destinatie Danubius",
    "129 (re) Casa Sarca-Sorescu",
    "130 Casa Suta-Wonderland",
    "131 PUD Oasului Hufi",
    "132 Reabilitare Camin Cultural Baciu",
    "133 PUG Feldru",
    "134 DALI Blocuri Murfatlar",
    "135 Case de vacanta Dianei",
    "136 PUG BROSTENI",
    "139 Casa Turda",
    "140 Releveu Bistrita (Morariu)",
    "141 PUZ Janovic Jeno",
    "142 Penny Borhanci",
    "143 Pavilion Politie Radauti",
    "149 Duplex Sorescu 31-33",
    "150 DALI SF Scoala Baciu",
    "151 Casa Alexandru Bohatiel 17",
    "152 PUZ Penny Tautii Magheraus",
    "153 PUG Banita",
    "155 PT Scoala Floresti",
    "156 Case Sorescu",
    "157 Gradi-Cresa Baciu",
    "158 Duplex Sorescu 21-23",
    "159 Amenajare Spatiu Grenke PBC",
    "160 Etajare Primaria Baciu",
    "161 Extindere Ap Baciu",
    "164 SD salon Aurel Vlaicu",
    "165 Reclama Marasti",
    "166 Catei Apahida",
    "167 Apartament Mircea Zaciu 13-15",
    "169 Casa PETRILA 37",
    "170 Cabana Campeni AB",
    "171 Camin Apahida",
    "L089 PUZ TUSA-BOJAN",
    "172 Design casa Iugoslaviei 18",
    "173 Reabilitare spitale Sighetu",
    "174 StudX UMFST",
    "176 - 2025 - ReAC Ansamblu rezi Bibescu",
  ];

  for (const line of beletageProjects) {
    const parsed = parseProjectLine(line, "B");
    if (parsed) {
      tags.push({
        label: parsed.label,
        category: "project",
        scope: "company",
        companyId: "beletage" as CompanyId,
        projectCode: parsed.code,
        color: "#22B5AB",
      });
    }
  }

  // ── Urban Switch projects ──
  const urbanSwitchProjects = [
    "001 PUZ Sopor - Ansamblu Rezidential",
    "002 PUZ Borhanci Nord",
    "003 PUZ Zona Centrala Cluj",
    "004 PUG Floresti",
    "005 PUZ Dezmir - Zona Industriala",
    "006 PUZ Gilau Est",
    "007 PUZ Baciu - Extensie Intravilan",
    "008 PUG Apahida",
    "009 PUZ Iris - Reconversie",
    "010 PUZ Faget - Zona Turistica",
  ];

  for (const line of urbanSwitchProjects) {
    const parsed = parseProjectLine(line, "US");
    if (parsed) {
      tags.push({
        label: parsed.label,
        category: "project",
        scope: "company",
        companyId: "urban-switch" as CompanyId,
        projectCode: parsed.code,
        color: "#345476",
      });
    }
  }

  // ── Studii de Teren projects ──
  const studiiDeTerenProjects = [
    "001 Studiu Geo - Sopor Rezidential",
    "002 Studiu Geo - Borhanci Vila",
    "003 Studiu Geo - Floresti Ansamblu",
    "004 Ridicare Topo - Dezmir Industrial",
    "005 Studiu Geo - Gilau Est",
    "006 Ridicare Topo - Baciu Extensie",
    "007 Studiu Geo - Apahida Centru",
    "008 Ridicare Topo - Faget",
    "009 Studiu Geo - Iris Reconversie",
    "010 Studiu Geo - Turda Rezidential",
  ];

  for (const line of studiiDeTerenProjects) {
    const parsed = parseProjectLine(line, "SDT");
    if (parsed) {
      tags.push({
        label: parsed.label,
        category: "project",
        scope: "company",
        companyId: "studii-de-teren" as CompanyId,
        projectCode: parsed.code,
        color: "#0182A1",
      });
    }
  }

  // ── Phase tags ──
  const phases = [
    "CU",
    "Schita",
    "Avize",
    "PUD",
    "AO",
    "PUZ",
    "PUG",
    "DTAD",
    "DTAC",
    "PT",
    "Detalii de Executie",
    "Studii de fundamentare",
    "Regulament",
    "Parte desenata",
    "Parte scrisa",
    "Consultanta client",
    "Macheta",
    "Consultanta receptie",
    "Redactare",
    "Depunere",
    "Ridicare",
    "Verificare proiect",
    "Vizita santier",
  ];

  for (const phase of phases) {
    tags.push({
      label: phase,
      category: "phase",
      scope: "global",
      color: "#3b82f6",
    });
  }

  // ── Activity tags ──
  const activities = [
    "Ofertare",
    "Configurari",
    "Organizare initiala",
    "Pregatire Portofoliu",
    "Website",
    "Documentare",
    "Design grafic",
    "Design interior",
    "Design exterior",
    "Releveu",
    "Reclama",
    "Master MATDR",
    "Pauza de masa",
    "Timp personal",
    "Concediu",
    "Compensare overtime",
  ];

  for (const activity of activities) {
    tags.push({
      label: activity,
      category: "activity",
      scope: "global",
      color: "#8b5cf6",
    });
  }

  // ── Document type tags ──
  const docTypes = [
    "Contract",
    "Ofertă",
    "Factură",
    "Scrisoare",
    "Aviz",
    "Notă de comandă",
    "Raport",
    "Cerere",
    "Altele",
  ];

  for (const dt of docTypes) {
    tags.push({
      label: dt,
      category: "document-type",
      scope: "global",
      color: "#f59e0b",
    });
  }

  return tags;
}
