-- Create HIPAA audit logs table for tracking all PHI access
CREATE TABLE hipaa_audit_logs
(
    id              UUID PRIMARY KEY     DEFAULT uuid_generate_v4(),
    user_id         UUID        NOT NULL REFERENCES users (id),
    action_type     VARCHAR(100) NOT NULL CHECK (action_type IN ('VIEW', 'CREATE', 'UPDATE', 'DELETE', 'EXPORT', 'PRINT', 'DOWNLOAD')),
    resource_type   VARCHAR(100) NOT NULL, -- MEDICAL_RECORD, PRESCRIPTION, APPOINTMENT, VITAL_SIGNS, LAB_RESULT, PATIENT
    resource_id      UUID        NOT NULL,
    patient_id       UUID                 REFERENCES patients (id),
    ip_address       VARCHAR(45),
    user_agent      TEXT,
    timestamp        TIMESTAMP   NOT NULL DEFAULT CURRENT_TIMESTAMP,
    details          JSONB,               -- Additional context information
    success          BOOLEAN     DEFAULT TRUE,
    error_message    TEXT
);

-- Create PHI access logs table for detailed tracking of which PHI fields were accessed
CREATE TABLE phi_access_logs
(
    id              UUID PRIMARY KEY     DEFAULT uuid_generate_v4(),
    user_id         UUID        NOT NULL REFERENCES users (id),
    patient_id      UUID        NOT NULL REFERENCES patients (id),
    access_type     VARCHAR(50)  NOT NULL, -- Treatment, Payment, Operations, Authorization
    accessed_fields TEXT[],               -- Which PHI fields were accessed
    purpose         VARCHAR(255),        -- Reason for access
    timestamp       TIMESTAMP   NOT NULL DEFAULT CURRENT_TIMESTAMP,
    ip_address      VARCHAR(45),
    user_agent      TEXT
);

-- Create breach notifications table
CREATE TABLE breach_notifications
(
    id                      UUID PRIMARY KEY     DEFAULT uuid_generate_v4(),
    incident_date           DATE         NOT NULL,
    discovery_date          DATE         NOT NULL,
    breach_type             VARCHAR(100) NOT NULL, -- UNAUTHORIZED_ACCESS, DISCLOSURE, LOSS, THEFT
    affected_patients_count INTEGER,
    description             TEXT         NOT NULL,
    mitigation_steps        TEXT,
    notified_at             TIMESTAMP,
    status                  VARCHAR(50)  DEFAULT 'INVESTIGATING' CHECK (status IN ('INVESTIGATING', 'CONTAINED', 'RESOLVED', 'REPORTED')),
    created_by              UUID         NOT NULL REFERENCES users (id),
    created_at              TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at               TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- Create indexes for better query performance
CREATE INDEX idx_hipaa_audit_logs_user_id ON hipaa_audit_logs (user_id);
CREATE INDEX idx_hipaa_audit_logs_patient_id ON hipaa_audit_logs (patient_id);
CREATE INDEX idx_hipaa_audit_logs_resource_type ON hipaa_audit_logs (resource_type);
CREATE INDEX idx_hipaa_audit_logs_timestamp ON hipaa_audit_logs (timestamp DESC);
CREATE INDEX idx_hipaa_audit_logs_action_type ON hipaa_audit_logs (action_type);

CREATE INDEX idx_phi_access_logs_user_id ON phi_access_logs (user_id);
CREATE INDEX idx_phi_access_logs_patient_id ON phi_access_logs (patient_id);
CREATE INDEX idx_phi_access_logs_timestamp ON phi_access_logs (timestamp DESC);

CREATE INDEX idx_breach_notifications_status ON breach_notifications (status);
CREATE INDEX idx_breach_notifications_incident_date ON breach_notifications (incident_date DESC);

