CREATE TABLE IF NOT EXISTS appointments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    tenant_id INT NOT NULL,
    created_by_user_id INT NULL,
    appointment_uid VARCHAR(80) NOT NULL,
    type VARCHAR(40) NOT NULL,
    title VARCHAR(190) NOT NULL,
    client_source VARCHAR(40) NOT NULL DEFAULT 'existing',
    client_status VARCHAR(40) NOT NULL DEFAULT 'active',
    client_name VARCHAR(190) NOT NULL,
    potential_client_email VARCHAR(190) NULL,
    potential_client_phone VARCHAR(60) NULL,
    appointment_date DATE NOT NULL,
    start_time TIME NOT NULL,
    end_time TIME NOT NULL,
    duration_minutes INT NOT NULL DEFAULT 60,
    technician_id INT NULL,
    technician_name VARCHAR(190) NOT NULL,
    location VARCHAR(255) NULL,
    notes TEXT NULL,
    status VARCHAR(50) NOT NULL DEFAULT 'booked',
    created_by_demo TINYINT(1) NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    KEY appointments_tenant_date_idx (tenant_id, appointment_date, start_time),
    KEY appointments_tenant_technician_idx (tenant_id, technician_id, appointment_date)
);
