"""
Vytvoří tabulku covance_samples v databázi studie.
Spusť jednorázově pro inicializaci.
"""

import mysql.connector
import db_config

DDL = """
CREATE TABLE IF NOT EXISTS covance_samples (
    id                        INT AUTO_INCREMENT PRIMARY KEY,
    import_id                 INT NOT NULL,
    study                     VARCHAR(20)  NOT NULL,
    investigator_no           VARCHAR(10),
    investigator_name         VARCHAR(100),
    patient_no                VARCHAR(20),
    collection_date           DATE,
    protocol_visit_code       VARCHAR(20),
    kit_receipt_date          DATE,
    container_receipt_date    DATE,
    accession                 VARCHAR(20),
    container_no              TINYINT UNSIGNED,
    container_barcode         VARCHAR(30),
    specimen_type             VARCHAR(100),
    sample_status             VARCHAR(30),
    expected_receipt_condition VARCHAR(50),
    actual_receipt_condition  VARCHAR(100),
    label_line1               VARCHAR(100),
    label_line2               VARCHAR(100),
    sm_sample_status          VARCHAR(50),
    smart_class_description   VARCHAR(100),
    parent_barcode            VARCHAR(30),
    children_barcode          VARCHAR(30),
    FOREIGN KEY (import_id) REFERENCES iwrs_import(import_id),
    INDEX idx_import  (import_id),
    INDEX idx_patient (patient_no),
    INDEX idx_accession (accession),
    INDEX idx_barcode (container_barcode)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
"""

conn = mysql.connector.connect(
    host=db_config.DB_HOST, port=db_config.DB_PORT,
    user=db_config.DB_USER, password=db_config.DB_PASSWORD,
    database=db_config.DB_NAME,
)
cursor = conn.cursor()
cursor.execute(DDL)
conn.commit()
cursor.close()
conn.close()
print("Tabulka covance_samples OK.")
