import sqlite3

def table_exists(cursor, table_name):
    cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?;", (table_name,))
    return cursor.fetchone() is not None

def create_tables():
    conn = sqlite3.connect("database.sqlite")
    cursor = conn.cursor()

    tables = {
        "sheep": """
        CREATE TABLE IF NOT EXISTS sheep (
            id INTEGER PRIMARY KEY,
            group_id INTEGER,
            name TEXT,
            icon TEXT,
            hash TEXT,
            appointment TEXT DEFAULT 'lamb',
            mode INTEGER DEFAULT 0,
            cu_access INTEGER DEFAULT 0,
            mt_access INTEGER DEFAULT 0,
            ct_access INTEGER DEFAULT 0,
            ms_access INTEGER DEFAULT 0,
            sm_access INTEGER DEFAULT 0,
            FOREIGN KEY (group_id) REFERENCES groups(id)
        );""",
        "groups": """
        CREATE TABLE IF NOT EXISTS groups (
            id INTEGER PRIMARY KEY,
            group_number INTEGER,
            share_hash TEXT
        );""",
        "subscription": """
        CREATE TABLE IF NOT EXISTS subscription (
            id INTEGER PRIMARY KEY,
            sheep_id INTEGER,
            token TEXT,
            FOREIGN KEY (sheep_id) REFERENCES sheep(id)
        );""",
        "house": """
        CREATE TABLE IF NOT EXISTS house (
            id INTEGER PRIMARY KEY,
            group_id INTEGER,
            title TEXT,
            number TEXT,
            points TEXT DEFAULT '[]',
            points_number TEXT DEFAULT '[]',
            geo TEXT DEFAULT '[]',
            osm_id TEXT DEFAULT '[]',
            settlement TEXT,
            description TEXT,
            created_at TIMESTAMP,
            updated_at TIMESTAMP,
            FOREIGN KEY (group_id) REFERENCES groups(id)
        );""",
        "entrance": """
        CREATE TABLE IF NOT EXISTS entrance (
            id INTEGER PRIMARY KEY,
            house_id INTEGER,
            entrance_number INTEGER,
            title TEXT,
            points TEXT DEFAULT '[]',
            points_number TEXT DEFAULT '[]',
            floors_quantity TEXT,
            apartments_quantity TEXT,
            description TEXT,
            created_at TIMESTAMP,
            updated_at TIMESTAMP,
            FOREIGN KEY (house_id) REFERENCES house(id)
        );""",
        "entrance_history": """
        CREATE TABLE IF NOT EXISTS entrance_history (
            id INTEGER PRIMARY KEY,
            entrance_id INTEGER,
            name TEXT,
            date_start TIMESTAMP,
            date_end TIMESTAMP,
            group_number INTEGER,
            sheep_id TEXT,
            working INTEGER DEFAULT 0,
            FOREIGN KEY (entrance_id) REFERENCES entrance(id),
            FOREIGN KEY (sheep_id) REFERENCES sheep(id)
        );""",
        "apartments": """
        CREATE TABLE IF NOT EXISTS apartments (
            id INTEGER PRIMARY KEY,
            entrance_id INTEGER,
            apartment_number INTEGER,
            title TEXT,
            floors_number INTEGER,
            status INTEGER,
            description TEXT,
            sheep_id TEXT,
            updated_at TIMESTAMP,
            FOREIGN KEY (entrance_id) REFERENCES entrance(id),
            FOREIGN KEY (sheep_id) REFERENCES sheep(id)
        );""",
        "apartments_history": """
        CREATE TABLE IF NOT EXISTS apartments_history (
            id INTEGER PRIMARY KEY,
            apartments_id INTEGER,
            status INTEGER,
            description TEXT,
            sheep_id TEXT,
            created_at TIMESTAMP,
            FOREIGN KEY (apartments_id) REFERENCES apartments(id)
        );""",
        "homestead": """
        CREATE TABLE IF NOT EXISTS homestead (
            id INTEGER PRIMARY KEY,
            group_id INTEGER,
            title TEXT,
            number TEXT,
            points TEXT DEFAULT '[]',
            point_icons TEXT DEFAULT '[]',
            geo TEXT DEFAULT '[]',
            osm_id TEXT DEFAULT '[]',
            settlement TEXT,
            description TEXT,
            created_at TIMESTAMP,
            updated_at TIMESTAMP,
            FOREIGN KEY (group_id) REFERENCES groups(id)
        );""",
        "homestead_history": """
        CREATE TABLE IF NOT EXISTS homestead_history (
            id INTEGER PRIMARY KEY,
            homestead_id INTEGER,
            name TEXT,
            date_start TIMESTAMP,
            date_end TIMESTAMP,
            group_number INTEGER,
            sheep_id TEXT,
            working INTEGER DEFAULT 0,
            FOREIGN KEY (homestead_id) REFERENCES homestead(id),
            FOREIGN KEY (sheep_id) REFERENCES sheep(id)
        );""",
        "meetings_schedule": """
        CREATE TABLE IF NOT EXISTS meetings_schedule (
            id INTEGER PRIMARY KEY,
            date TIMESTAMP,
            type INTEGER,
            name TEXT,
            sheep_id TEXT,
            title TEXT,
            number TEXT,
            FOREIGN KEY (sheep_id) REFERENCES sheep(id)
        );""",
        "stand_list": """
        CREATE TABLE IF NOT EXISTS stand_list (
            id INTEGER PRIMARY KEY,
            title TEXT,
            hour_start INTEGER DEFAULT 10,
            hour_end INTEGER DEFAULT 16,
            quantity_sheep INTEGER DEFAULT 2,
            week_days TEXT DEFAULT '[0, 1, 2, 3, 4, 5, 6]'
        );""",
        "stand_schedule": """
        CREATE TABLE IF NOT EXISTS stand_schedule (
            id INTEGER PRIMARY KEY,
            stand INTEGER,
            date TIMESTAMP,
            hour INTEGER,
            sheep_id TEXT,
            number_sheep TEXT,
            updated_at TIMESTAMP,
            FOREIGN KEY (sheep_id) REFERENCES sheep(id),
            FOREIGN KEY (stand) REFERENCES stand_list(id)
        );"""
    }

    for name, sql in tables.items():
        if not table_exists(cursor, name):
            cursor.execute(sql)
    
    conn.commit()
    conn.close()

if __name__ == "__main__":
    create_tables()
    print("Database setup complete.")