-- H-01: explicit ON DELETE actions for previously-implicit NO ACTION FKs.
--
-- Without an explicit action Postgres defaults to NO ACTION, so a hard-
-- delete of a parent (client, port, berth, file, document signer) is
-- blocked at FK check time - sometimes intentional, often surprising.
-- Each FK below now declares whether parent deletion is RESTRICT (block,
-- force the operator to archive the parent or unlink the children first)
-- or SET NULL (allow the deletion, null the FK so child rows stay around
-- as historical records).
--
-- All ALTER COLUMNs are idempotent because we drop the constraint first
-- (if it exists) and re-add it under the same name; if the constraint is
-- already in the desired shape this is a no-op against Postgres.

-- interests: required parent links; archive-first is the supported path,
-- so RESTRICT a hard-delete to surface the misuse loudly.
ALTER TABLE interests DROP CONSTRAINT IF EXISTS interests_port_id_ports_id_fk;
ALTER TABLE interests
  ADD CONSTRAINT interests_port_id_ports_id_fk
  FOREIGN KEY (port_id) REFERENCES ports(id) ON DELETE RESTRICT;

ALTER TABLE interests DROP CONSTRAINT IF EXISTS interests_client_id_clients_id_fk;
ALTER TABLE interests
  ADD CONSTRAINT interests_client_id_clients_id_fk
  FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE RESTRICT;

-- documents: client/file links are nullable already; tolerate parent
-- deletion via SET NULL so the document row stays for audit purposes.
ALTER TABLE documents DROP CONSTRAINT IF EXISTS documents_client_id_clients_id_fk;
ALTER TABLE documents
  ADD CONSTRAINT documents_client_id_clients_id_fk
  FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE SET NULL;

ALTER TABLE documents DROP CONSTRAINT IF EXISTS documents_file_id_files_id_fk;
ALTER TABLE documents
  ADD CONSTRAINT documents_file_id_files_id_fk
  FOREIGN KEY (file_id) REFERENCES files(id) ON DELETE SET NULL;

ALTER TABLE documents DROP CONSTRAINT IF EXISTS documents_signed_file_id_files_id_fk;
ALTER TABLE documents
  ADD CONSTRAINT documents_signed_file_id_files_id_fk
  FOREIGN KEY (signed_file_id) REFERENCES files(id) ON DELETE SET NULL;

-- document_events: outlive their signer row so the audit trail stays
-- intact when a recipient is removed.
ALTER TABLE document_events DROP CONSTRAINT IF EXISTS document_events_signer_id_document_signers_id_fk;
ALTER TABLE document_events
  ADD CONSTRAINT document_events_signer_id_document_signers_id_fk
  FOREIGN KEY (signer_id) REFERENCES document_signers(id) ON DELETE SET NULL;

-- berth_reservations: every parent FK gets RESTRICT (canonical occupancy
-- record; never silently orphaned). interestId is nullable and SET NULL
-- so a reservation legitimately outlives the originating deal.
ALTER TABLE berth_reservations DROP CONSTRAINT IF EXISTS berth_reservations_berth_id_berths_id_fk;
ALTER TABLE berth_reservations
  ADD CONSTRAINT berth_reservations_berth_id_berths_id_fk
  FOREIGN KEY (berth_id) REFERENCES berths(id) ON DELETE RESTRICT;

ALTER TABLE berth_reservations DROP CONSTRAINT IF EXISTS berth_reservations_port_id_ports_id_fk;
ALTER TABLE berth_reservations
  ADD CONSTRAINT berth_reservations_port_id_ports_id_fk
  FOREIGN KEY (port_id) REFERENCES ports(id) ON DELETE RESTRICT;

ALTER TABLE berth_reservations DROP CONSTRAINT IF EXISTS berth_reservations_client_id_clients_id_fk;
ALTER TABLE berth_reservations
  ADD CONSTRAINT berth_reservations_client_id_clients_id_fk
  FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE RESTRICT;

ALTER TABLE berth_reservations DROP CONSTRAINT IF EXISTS berth_reservations_yacht_id_yachts_id_fk;
ALTER TABLE berth_reservations
  ADD CONSTRAINT berth_reservations_yacht_id_yachts_id_fk
  FOREIGN KEY (yacht_id) REFERENCES yachts(id) ON DELETE RESTRICT;

ALTER TABLE berth_reservations DROP CONSTRAINT IF EXISTS berth_reservations_interest_id_interests_id_fk;
ALTER TABLE berth_reservations
  ADD CONSTRAINT berth_reservations_interest_id_interests_id_fk
  FOREIGN KEY (interest_id) REFERENCES interests(id) ON DELETE SET NULL;

ALTER TABLE berth_reservations DROP CONSTRAINT IF EXISTS berth_reservations_contract_file_id_files_id_fk;
ALTER TABLE berth_reservations
  ADD CONSTRAINT berth_reservations_contract_file_id_files_id_fk
  FOREIGN KEY (contract_file_id) REFERENCES files(id) ON DELETE SET NULL;

-- reminders.client_id: nullable, tolerate parent delete with SET NULL.
ALTER TABLE reminders DROP CONSTRAINT IF EXISTS reminders_client_id_clients_id_fk;
ALTER TABLE reminders
  ADD CONSTRAINT reminders_client_id_clients_id_fk
  FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE SET NULL;

-- invoices.pdf_file_id: nullable, tolerate parent delete with SET NULL.
ALTER TABLE invoices DROP CONSTRAINT IF EXISTS invoices_pdf_file_id_files_id_fk;
ALTER TABLE invoices
  ADD CONSTRAINT invoices_pdf_file_id_files_id_fk
  FOREIGN KEY (pdf_file_id) REFERENCES files(id) ON DELETE SET NULL;
