-- Rename all tables, columns, enum types, and constraints from Prisma's
-- default PascalCase/camelCase to standard Postgres snake_case, via @@map /
-- @map in schema.prisma. Uses RENAME everywhere (never DROP/CREATE) so
-- existing data and row identities are preserved.

-- 1. Enum types
ALTER TYPE "UserRole" RENAME TO user_role;
ALTER TYPE "UserStatus" RENAME TO user_status;
ALTER TYPE "LegalDocType" RENAME TO legal_doc_type;
ALTER TYPE "TenantStatus" RENAME TO tenant_status;
ALTER TYPE "DiagnosticStatus" RENAME TO diagnostic_status;
ALTER TYPE "InvitationStatus" RENAME TO invitation_status;
ALTER TYPE "AlertSeverity" RENAME TO alert_severity;
ALTER TYPE "AlertStatus" RENAME TO alert_status;
ALTER TYPE "ReportStatus" RENAME TO report_status;
ALTER TYPE "InstrumentVersionStatus" RENAME TO instrument_version_status;
ALTER TYPE "PillarKey" RENAME TO pillar_key;

-- 2. Tables
ALTER TABLE "User" RENAME TO users;
ALTER TABLE "Session" RENAME TO sessions;
ALTER TABLE "PasswordResetToken" RENAME TO password_reset_tokens;
ALTER TABLE "LegalAcceptance" RENAME TO legal_acceptances;
ALTER TABLE "UserInvitation" RENAME TO user_invitations;
ALTER TABLE "Tenant" RENAME TO tenants;
ALTER TABLE "Diagnostic" RENAME TO diagnostics;
ALTER TABLE "Brief" RENAME TO briefs;
ALTER TABLE "BriefMeeting" RENAME TO brief_meetings;
ALTER TABLE "BriefParticipant" RENAME TO brief_participants;
ALTER TABLE "BriefKpi" RENAME TO brief_kpis;
ALTER TABLE "Invitation" RENAME TO invitations;
ALTER TABLE "Response" RENAME TO responses;
ALTER TABLE "Alert" RENAME TO alerts;
ALTER TABLE "Report" RENAME TO reports;
ALTER TABLE "InstrumentVersion" RENAME TO instrument_versions;
ALTER TABLE "InstrumentPillar" RENAME TO instrument_pillars;
ALTER TABLE "InstrumentItem" RENAME TO instrument_items;
ALTER TABLE "InstrumentOpenQuestion" RENAME TO instrument_open_questions;

-- 3. Columns
ALTER TABLE users RENAME COLUMN "passwordHash" TO password_hash;
ALTER TABLE users RENAME COLUMN "tenantId" TO tenant_id;
ALTER TABLE users RENAME COLUMN "createdAt" TO created_at;
ALTER TABLE users RENAME COLUMN "updatedAt" TO updated_at;

ALTER TABLE sessions RENAME COLUMN "userId" TO user_id;
ALTER TABLE sessions RENAME COLUMN "tokenHash" TO token_hash;
ALTER TABLE sessions RENAME COLUMN "ipAddress" TO ip_address;
ALTER TABLE sessions RENAME COLUMN "userAgent" TO user_agent;
ALTER TABLE sessions RENAME COLUMN "expiresAt" TO expires_at;
ALTER TABLE sessions RENAME COLUMN "revokedAt" TO revoked_at;
ALTER TABLE sessions RENAME COLUMN "createdAt" TO created_at;

ALTER TABLE password_reset_tokens RENAME COLUMN "userId" TO user_id;
ALTER TABLE password_reset_tokens RENAME COLUMN "tokenHash" TO token_hash;
ALTER TABLE password_reset_tokens RENAME COLUMN "expiresAt" TO expires_at;
ALTER TABLE password_reset_tokens RENAME COLUMN "usedAt" TO used_at;
ALTER TABLE password_reset_tokens RENAME COLUMN "createdAt" TO created_at;

ALTER TABLE legal_acceptances RENAME COLUMN "userId" TO user_id;
ALTER TABLE legal_acceptances RENAME COLUMN "documentType" TO document_type;
ALTER TABLE legal_acceptances RENAME COLUMN "acceptedAt" TO accepted_at;
ALTER TABLE legal_acceptances RENAME COLUMN "ipHash" TO ip_hash;

ALTER TABLE user_invitations RENAME COLUMN "tenantId" TO tenant_id;
ALTER TABLE user_invitations RENAME COLUMN "invitedById" TO invited_by_id;
ALTER TABLE user_invitations RENAME COLUMN "expiresAt" TO expires_at;
ALTER TABLE user_invitations RENAME COLUMN "acceptedAt" TO accepted_at;
ALTER TABLE user_invitations RENAME COLUMN "createdAt" TO created_at;

ALTER TABLE tenants RENAME COLUMN "legalName" TO legal_name;
ALTER TABLE tenants RENAME COLUMN "logoUrl" TO logo_url;
ALTER TABLE tenants RENAME COLUMN "kAnonMin" TO k_anon_min;
ALTER TABLE tenants RENAME COLUMN "retentionMonths" TO retention_months;
ALTER TABLE tenants RENAME COLUMN "reportRecipientEmail" TO report_recipient_email;
ALTER TABLE tenants RENAME COLUMN "createdAt" TO created_at;
ALTER TABLE tenants RENAME COLUMN "updatedAt" TO updated_at;

ALTER TABLE diagnostics RENAME COLUMN "tenantId" TO tenant_id;
ALTER TABLE diagnostics RENAME COLUMN "slotsExpected" TO slots_expected;
ALTER TABLE diagnostics RENAME COLUMN "opensAt" TO opens_at;
ALTER TABLE diagnostics RENAME COLUMN "closesAt" TO closes_at;
ALTER TABLE diagnostics RENAME COLUMN "segmentConfig" TO segment_config;
ALTER TABLE diagnostics RENAME COLUMN "instrumentVersionId" TO instrument_version_id;
ALTER TABLE diagnostics RENAME COLUMN "createdAt" TO created_at;
ALTER TABLE diagnostics RENAME COLUMN "updatedAt" TO updated_at;

ALTER TABLE briefs RENAME COLUMN "diagnosticId" TO diagnostic_id;
ALTER TABLE briefs RENAME COLUMN "mainPain" TO main_pain;
ALTER TABLE briefs RENAME COLUMN "pilotObjective" TO pilot_objective;
ALTER TABLE briefs RENAME COLUMN "targetPopulation" TO target_population;
ALTER TABLE briefs RENAME COLUMN "sensitivePillars" TO sensitive_pillars;
ALTER TABLE briefs RENAME COLUMN "processRisks" TO process_risks;
ALTER TABLE briefs RENAME COLUMN "confidentialityAgreements" TO confidentiality_agreements;
ALTER TABLE briefs RENAME COLUMN "nextSteps" TO next_steps;
ALTER TABLE briefs RENAME COLUMN "followUpAt" TO follow_up_at;
ALTER TABLE briefs RENAME COLUMN "createdAt" TO created_at;
ALTER TABLE briefs RENAME COLUMN "updatedAt" TO updated_at;

ALTER TABLE brief_meetings RENAME COLUMN "briefId" TO brief_id;
ALTER TABLE brief_meetings RENAME COLUMN "createdAt" TO created_at;

ALTER TABLE brief_participants RENAME COLUMN "briefMeetingId" TO brief_meeting_id;

ALTER TABLE brief_kpis RENAME COLUMN "briefId" TO brief_id;

ALTER TABLE invitations RENAME COLUMN "diagnosticId" TO diagnostic_id;
ALTER TABLE invitations RENAME COLUMN "lastSentAt" TO last_sent_at;
ALTER TABLE invitations RENAME COLUMN "answeredAt" TO answered_at;
ALTER TABLE invitations RENAME COLUMN "expiresAt" TO expires_at;
ALTER TABLE invitations RENAME COLUMN "createdAt" TO created_at;

ALTER TABLE responses RENAME COLUMN "diagnosticId" TO diagnostic_id;
ALTER TABLE responses RENAME COLUMN "invitationId" TO invitation_id;
ALTER TABLE responses RENAME COLUMN "segmentArea" TO segment_area;
ALTER TABLE responses RENAME COLUMN "segmentRole" TO segment_role;
ALTER TABLE responses RENAME COLUMN "segmentModality" TO segment_modality;
ALTER TABLE responses RENAME COLUMN "segmentTenure" TO segment_tenure;
ALTER TABLE responses RENAME COLUMN "segmentTeam" TO segment_team;
ALTER TABLE responses RENAME COLUMN "pulseScore" TO pulse_score;
ALTER TABLE responses RENAME COLUMN "submittedAt" TO submitted_at;

ALTER TABLE alerts RENAME COLUMN "diagnosticId" TO diagnostic_id;
ALTER TABLE alerts RENAME COLUMN "tenantId" TO tenant_id;
ALTER TABLE alerts RENAME COLUMN "excerptAnon" TO excerpt_anon;
ALTER TABLE alerts RENAME COLUMN "detectedAt" TO detected_at;
ALTER TABLE alerts RENAME COLUMN "resolvedAt" TO resolved_at;

ALTER TABLE reports RENAME COLUMN "diagnosticId" TO diagnostic_id;
ALTER TABLE reports RENAME COLUMN "executiveSummary" TO executive_summary;
ALTER TABLE reports RENAME COLUMN "technicalLimitations" TO technical_limitations;
ALTER TABLE reports RENAME COLUMN "pdfUrl" TO pdf_url;
ALTER TABLE reports RENAME COLUMN "signedAt" TO signed_at;
ALTER TABLE reports RENAME COLUMN "signedByUserId" TO signed_by_user_id;
ALTER TABLE reports RENAME COLUMN "sentAt" TO sent_at;
ALTER TABLE reports RENAME COLUMN "updatedAt" TO updated_at;
ALTER TABLE reports RENAME COLUMN "createdAt" TO created_at;

ALTER TABLE instrument_versions RENAME COLUMN "publishedAt" TO published_at;
ALTER TABLE instrument_versions RENAME COLUMN "createdAt" TO created_at;
ALTER TABLE instrument_versions RENAME COLUMN "updatedAt" TO updated_at;

ALTER TABLE instrument_pillars RENAME COLUMN "versionId" TO version_id;

ALTER TABLE instrument_items RENAME COLUMN "pillarId" TO pillar_id;
ALTER TABLE instrument_items RENAME COLUMN "scaleLabels" TO scale_labels;
ALTER TABLE instrument_items RENAME COLUMN "createdAt" TO created_at;
ALTER TABLE instrument_items RENAME COLUMN "updatedAt" TO updated_at;

ALTER TABLE instrument_open_questions RENAME COLUMN "versionId" TO version_id;
ALTER TABLE instrument_open_questions RENAME COLUMN "pillarKey" TO pillar_key;

-- 4. Primary key constraints
ALTER TABLE users RENAME CONSTRAINT "User_pkey" TO users_pkey;
ALTER TABLE sessions RENAME CONSTRAINT "Session_pkey" TO sessions_pkey;
ALTER TABLE password_reset_tokens RENAME CONSTRAINT "PasswordResetToken_pkey" TO password_reset_tokens_pkey;
ALTER TABLE legal_acceptances RENAME CONSTRAINT "LegalAcceptance_pkey" TO legal_acceptances_pkey;
ALTER TABLE user_invitations RENAME CONSTRAINT "UserInvitation_pkey" TO user_invitations_pkey;
ALTER TABLE tenants RENAME CONSTRAINT "Tenant_pkey" TO tenants_pkey;
ALTER TABLE diagnostics RENAME CONSTRAINT "Diagnostic_pkey" TO diagnostics_pkey;
ALTER TABLE briefs RENAME CONSTRAINT "Brief_pkey" TO briefs_pkey;
ALTER TABLE brief_meetings RENAME CONSTRAINT "BriefMeeting_pkey" TO brief_meetings_pkey;
ALTER TABLE brief_participants RENAME CONSTRAINT "BriefParticipant_pkey" TO brief_participants_pkey;
ALTER TABLE brief_kpis RENAME CONSTRAINT "BriefKpi_pkey" TO brief_kpis_pkey;
ALTER TABLE invitations RENAME CONSTRAINT "Invitation_pkey" TO invitations_pkey;
ALTER TABLE responses RENAME CONSTRAINT "Response_pkey" TO responses_pkey;
ALTER TABLE alerts RENAME CONSTRAINT "Alert_pkey" TO alerts_pkey;
ALTER TABLE reports RENAME CONSTRAINT "Report_pkey" TO reports_pkey;
ALTER TABLE instrument_versions RENAME CONSTRAINT "InstrumentVersion_pkey" TO instrument_versions_pkey;
ALTER TABLE instrument_pillars RENAME CONSTRAINT "InstrumentPillar_pkey" TO instrument_pillars_pkey;
ALTER TABLE instrument_items RENAME CONSTRAINT "InstrumentItem_pkey" TO instrument_items_pkey;
ALTER TABLE instrument_open_questions RENAME CONSTRAINT "InstrumentOpenQuestion_pkey" TO instrument_open_questions_pkey;

-- 5. Foreign key constraints
ALTER TABLE users RENAME CONSTRAINT "User_tenantId_fkey" TO users_tenant_id_fkey;
ALTER TABLE sessions RENAME CONSTRAINT "Session_userId_fkey" TO sessions_user_id_fkey;
ALTER TABLE password_reset_tokens RENAME CONSTRAINT "PasswordResetToken_userId_fkey" TO password_reset_tokens_user_id_fkey;
ALTER TABLE legal_acceptances RENAME CONSTRAINT "LegalAcceptance_userId_fkey" TO legal_acceptances_user_id_fkey;
ALTER TABLE user_invitations RENAME CONSTRAINT "UserInvitation_tenantId_fkey" TO user_invitations_tenant_id_fkey;
ALTER TABLE diagnostics RENAME CONSTRAINT "Diagnostic_tenantId_fkey" TO diagnostics_tenant_id_fkey;
ALTER TABLE diagnostics RENAME CONSTRAINT "Diagnostic_instrumentVersionId_fkey" TO diagnostics_instrument_version_id_fkey;
ALTER TABLE briefs RENAME CONSTRAINT "Brief_diagnosticId_fkey" TO briefs_diagnostic_id_fkey;
ALTER TABLE brief_meetings RENAME CONSTRAINT "BriefMeeting_briefId_fkey" TO brief_meetings_brief_id_fkey;
ALTER TABLE brief_participants RENAME CONSTRAINT "BriefParticipant_briefMeetingId_fkey" TO brief_participants_brief_meeting_id_fkey;
ALTER TABLE brief_kpis RENAME CONSTRAINT "BriefKpi_briefId_fkey" TO brief_kpis_brief_id_fkey;
ALTER TABLE invitations RENAME CONSTRAINT "Invitation_diagnosticId_fkey" TO invitations_diagnostic_id_fkey;
ALTER TABLE responses RENAME CONSTRAINT "Response_diagnosticId_fkey" TO responses_diagnostic_id_fkey;
ALTER TABLE responses RENAME CONSTRAINT "Response_invitationId_fkey" TO responses_invitation_id_fkey;
ALTER TABLE alerts RENAME CONSTRAINT "Alert_diagnosticId_fkey" TO alerts_diagnostic_id_fkey;
ALTER TABLE reports RENAME CONSTRAINT "Report_diagnosticId_fkey" TO reports_diagnostic_id_fkey;
ALTER TABLE instrument_pillars RENAME CONSTRAINT "InstrumentPillar_versionId_fkey" TO instrument_pillars_version_id_fkey;
ALTER TABLE instrument_items RENAME CONSTRAINT "InstrumentItem_pillarId_fkey" TO instrument_items_pillar_id_fkey;
ALTER TABLE instrument_open_questions RENAME CONSTRAINT "InstrumentOpenQuestion_versionId_fkey" TO instrument_open_questions_version_id_fkey;

-- 6. Unique indexes
ALTER INDEX "User_email_key" RENAME TO users_email_key;
ALTER INDEX "Session_tokenHash_key" RENAME TO sessions_token_hash_key;
ALTER INDEX "PasswordResetToken_tokenHash_key" RENAME TO password_reset_tokens_token_hash_key;
ALTER INDEX "UserInvitation_token_key" RENAME TO user_invitations_token_key;
ALTER INDEX "Tenant_slug_key" RENAME TO tenants_slug_key;
ALTER INDEX "Diagnostic_code_key" RENAME TO diagnostics_code_key;
ALTER INDEX "Brief_diagnosticId_key" RENAME TO briefs_diagnostic_id_key;
ALTER INDEX "Invitation_token_key" RENAME TO invitations_token_key;
ALTER INDEX "Response_invitationId_key" RENAME TO responses_invitation_id_key;
ALTER INDEX "Report_diagnosticId_key" RENAME TO reports_diagnostic_id_key;
ALTER INDEX "InstrumentVersion_version_key" RENAME TO instrument_versions_version_key;
ALTER INDEX "InstrumentPillar_versionId_key_key" RENAME TO instrument_pillars_version_id_key_key;
