PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS directory_contacts (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  kind TEXT NOT NULL DEFAULT 'person' CHECK(kind IN ('person','organization')),
  name TEXT NOT NULL,
  phone TEXT,
  email TEXT,
  address TEXT,
  postal_code TEXT,
  city TEXT,
  country TEXT NOT NULL DEFAULT 'France',
  organization_id INTEGER,
  notes TEXT,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now')),
  deleted_at TEXT,
  FOREIGN KEY(organization_id) REFERENCES directory_contacts(id) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS directory_contact_roles (
  contact_id INTEGER NOT NULL,
  role TEXT NOT NULL CHECK(role IN ('fa','benevole','dirigeant','veterinaire','cabinet','crematorium','adoptant')),
  PRIMARY KEY(contact_id, role),
  FOREIGN KEY(contact_id) REFERENCES directory_contacts(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_directory_name ON directory_contacts(name);
CREATE INDEX IF NOT EXISTS idx_directory_role ON directory_contact_roles(role, contact_id);

ALTER TABLE medical_notes ADD COLUMN vet_contact_id INTEGER REFERENCES directory_contacts(id) ON DELETE SET NULL;
ALTER TABLE medical_notes ADD COLUMN clinic_contact_id INTEGER REFERENCES directory_contacts(id) ON DELETE SET NULL;
ALTER TABLE treatments ADD COLUMN vet_contact_id INTEGER REFERENCES directory_contacts(id) ON DELETE SET NULL;
ALTER TABLE treatments ADD COLUMN clinic_contact_id INTEGER REFERENCES directory_contacts(id) ON DELETE SET NULL;
ALTER TABLE vaccinations ADD COLUMN vet_contact_id INTEGER REFERENCES directory_contacts(id) ON DELETE SET NULL;
ALTER TABLE vaccinations ADD COLUMN clinic_contact_id INTEGER REFERENCES directory_contacts(id) ON DELETE SET NULL;
ALTER TABLE adoptions ADD COLUMN adopter_contact_id INTEGER REFERENCES directory_contacts(id) ON DELETE SET NULL;

INSERT INTO directory_contacts(kind, name)
SELECT 'organization', c.name FROM clinics c
WHERE NOT EXISTS (SELECT 1 FROM directory_contacts dc WHERE dc.kind='organization' AND lower(dc.name)=lower(c.name));
INSERT OR IGNORE INTO directory_contact_roles(contact_id, role)
SELECT dc.id, 'cabinet' FROM directory_contacts dc JOIN clinics c ON lower(c.name)=lower(dc.name);

INSERT INTO directory_contacts(kind, name, phone, email, address, postal_code, city)
SELECT 'person', a.adopter_name, MAX(a.adopter_phone), MAX(a.adopter_email), MAX(a.adopter_address), MAX(a.adopter_postal_code), MAX(a.adopter_city)
FROM adoptions a
WHERE NOT EXISTS (
  SELECT 1 FROM directory_contacts dc
  WHERE dc.kind='person' AND lower(dc.name)=lower(a.adopter_name)
    AND lower(COALESCE(dc.email,''))=lower(COALESCE(a.adopter_email,''))
)
GROUP BY lower(a.adopter_name), lower(COALESCE(a.adopter_email,''));
INSERT OR IGNORE INTO directory_contact_roles(contact_id, role)
SELECT DISTINCT dc.id, 'adoptant' FROM directory_contacts dc JOIN adoptions a
ON dc.kind='person' AND lower(dc.name)=lower(a.adopter_name)
AND lower(COALESCE(dc.email,''))=lower(COALESCE(a.adopter_email,''));
UPDATE adoptions SET adopter_contact_id = (
  SELECT dc.id FROM directory_contacts dc
  WHERE dc.kind='person' AND lower(dc.name)=lower(adoptions.adopter_name)
    AND lower(COALESCE(dc.email,''))=lower(COALESCE(adoptions.adopter_email,'')) LIMIT 1
) WHERE adopter_contact_id IS NULL;

INSERT INTO directory_contacts(kind, name)
SELECT 'person', vet_name FROM (
  SELECT vet_name FROM medical_notes UNION SELECT vet_name FROM treatments UNION SELECT vet_name FROM vaccinations
) vets
WHERE vet_name IS NOT NULL AND trim(vet_name)!=''
AND NOT EXISTS (SELECT 1 FROM directory_contacts dc WHERE dc.kind='person' AND lower(dc.name)=lower(vets.vet_name));
INSERT OR IGNORE INTO directory_contact_roles(contact_id, role)
SELECT dc.id, 'veterinaire' FROM directory_contacts dc
WHERE EXISTS (
  SELECT 1 FROM medical_notes WHERE lower(vet_name)=lower(dc.name)
  UNION SELECT 1 FROM treatments WHERE lower(vet_name)=lower(dc.name)
  UNION SELECT 1 FROM vaccinations WHERE lower(vet_name)=lower(dc.name)
);
