-- Patients Table
-- Description: Stores patient information, including personal details and medical history.
CREATE TABLE patients (
    tenant_id UUID,
    patient_id UUID DEFAULT gen_random_uuid(),
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100) NOT NULL,
    date_of_birth DATE NOT NULL,
    gender VARCHAR(10),
    contact_info JSONB,
    medical_history TEXT,
    vector_embedding VECTOR(768), -- Adjust the dimensions as needed
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (tenant_id, patient_id),
    FOREIGN KEY (tenant_id) REFERENCES tenants(id)
);
-- Departments Table
-- Description: Stores information about departments within the healthcare organization.
CREATE TABLE departments (
    tenant_id UUID,
    department_id UUID DEFAULT gen_random_uuid(),
    department_name VARCHAR(100),
    description TEXT,
    PRIMARY KEY (tenant_id, department_id),
    FOREIGN KEY (tenant_id) REFERENCES tenants(id)
);
-- Doctors Table
-- Description: Stores information about doctors, including their specializations and contact details.
CREATE TABLE doctors (
    tenant_id UUID,
    doctor_id UUID DEFAULT gen_random_uuid(),
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100) NOT NULL,
    specialization VARCHAR(100),
    contact_info JSONB,
    department_id UUID,
    PRIMARY KEY (tenant_id, doctor_id),
    FOREIGN KEY (tenant_id, doctor_id) REFERENCES users.tenant_users(tenant_id, user_id),
    FOREIGN KEY (tenant_id, department_id) REFERENCES departments(tenant_id, department_id)
);
-- Medical Records Table
-- Description: Stores detailed medical records for each patient.
CREATE TABLE medical_records (
    tenant_id UUID,
    record_id UUID DEFAULT gen_random_uuid(),
    patient_id UUID,
    record_date TIMESTAMP,
    description TEXT,
    record_data JSONB,
    vector_embedding VECTOR(768), -- Adjust the dimensions as needed
    PRIMARY KEY (tenant_id, record_id),
    FOREIGN KEY (tenant_id, patient_id) REFERENCES patients(tenant_id, patient_id)
);
-- Test Results Table
-- Description: Stores test results associated with medical records.
CREATE TABLE test_results (
    tenant_id UUID,
    test_id UUID DEFAULT gen_random_uuid(),
    record_id UUID,
    test_name VARCHAR(100),
    test_date TIMESTAMP,
    results JSONB,
    vector_embedding VECTOR(768), -- Adjust the dimensions as needed
    PRIMARY KEY (tenant_id, test_id),
    FOREIGN KEY (tenant_id, record_id) REFERENCES medical_records(tenant_id, record_id)
);
-- Appointments Table
-- Description: Tracks appointments for patients, including notes and status.
CREATE TABLE appointments (
    tenant_id UUID,
    appointment_id UUID DEFAULT gen_random_uuid(),
    patient_id UUID,
    appointment_date TIMESTAMP,
    doctor_id UUID,
    notes TEXT,
    status VARCHAR(50),
    PRIMARY KEY (tenant_id, appointment_id),
    FOREIGN KEY (tenant_id, patient_id) REFERENCES patients(tenant_id, patient_id),
    FOREIGN KEY (tenant_id, doctor_id) REFERENCES users.tenant_users(tenant_id, user_id)
);