-- PostgreSQL ga kirish
psql -U azamjon -d sanoqchibot

-- Superadminlar jadvali
CREATE TABLE IF NOT EXISTS sanoqchibotadmin (
    id SERIAL PRIMARY KEY,
    superadmin_fio varchar(255) NOT NULL,
    telegram_id BIGINT NOT NULL UNIQUE
);

-- Foydalanuvchilar jadvali
CREATE TABLE IF NOT EXISTS users (
    id SERIAL PRIMARY KEY,
    username TEXT,
    first_name TEXT,
    last_name TEXT,
    telegram_id BIGINT NOT NULL UNIQUE,
    join_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    referred_by TEXT
);

-- Guruhlar jadvali
CREATE TABLE IF NOT EXISTS groups (
    group_id BIGINT PRIMARY KEY,
    title TEXT,
    username TEXT,
    linked_channel TEXT,
    block_bots INTEGER DEFAULT 0,
    block_ads INTEGER DEFAULT 0,
    block_phone_number INTEGER DEFAULT 0,
    required_members INTEGER DEFAULT 0,
    is_admin_confirmed INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    welcome_message TEXT DEFAULT '0',
    forcedtext TEXT DEFAULT '0'
);

-- A'zolar soni jadvali
CREATE TABLE IF NOT EXISTS members_count (
    id SERIAL PRIMARY KEY,
    user_id BIGINT,
    group_id BIGINT,
    count BIGINT DEFAULT 0,
    UNIQUE(user_id, group_id)
);

-- Imtiyozli foydalanuvchilar
CREATE TABLE IF NOT EXISTS privileged_users (
    id SERIAL PRIMARY KEY,
    user_id BIGINT,
    group_id BIGINT,
    granted_by BIGINT,
    granted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(user_id, group_id)
);

-- Mute qilingan foydalanuvchilar
CREATE TABLE IF NOT EXISTS muted_users (
    id SERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL,
    group_id BIGINT NOT NULL,
    muted_until TIMESTAMP WITH TIME ZONE,
    UNIQUE(user_id, group_id)
);

-- Majburiy matnlar
CREATE TABLE IF NOT EXISTS textforce (
    id SERIAL PRIMARY KEY,
    admin_id BIGINT NOT NULL,
    textforce TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);