import psycopg2

def run():
    try:
        conn = psycopg2.connect(
            host="66.116.207.225",
            port="6432",
            database="nearledb",
            user="admin",
            password="Package@123#"
        )
        cursor = conn.cursor()

        sql = """
        CREATE TABLE IF NOT EXISTS rider_substitutions (
            id SERIAL PRIMARY KEY,
            tenant_id INT NOT NULL,
            sub_date DATE NOT NULL,
            absent_rider_id INT NOT NULL,
            absent_rider_name VARCHAR(100) NOT NULL,
            sub_rider_id INT NOT NULL,
            sub_rider_name VARCHAR(100) NOT NULL,
            reason VARCHAR(255) NULL,
            status VARCHAR(50) NOT NULL DEFAULT 'scheduled',
            created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            UNIQUE(tenant_id, sub_date, absent_rider_id)
        );
        """
        cursor.execute(sql)
        conn.commit()
        print("Table 'rider_substitutions' successfully created in remote database!")

    except Exception as e:
        print(f"Error: {e}")
    finally:
        if 'cursor' in locals():
            cursor.close()
        if 'conn' in locals():
            conn.close()

if __name__ == "__main__":
    run()
