-- Task Team — Initial Schema — 2026-03-29

-- Extensions
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS pgcrypto;

-- Users
CREATE TABLE users (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    email VARCHAR(255) UNIQUE,
    phone VARCHAR(50),
    name VARCHAR(255) NOT NULL,
    avatar_url TEXT,
    auth_provider VARCHAR(50) DEFAULT 'email',
    auth_provider_id TEXT,
    language VARCHAR(5) DEFAULT 'cs',
    settings JSONB DEFAULT '{}',
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Task groups
CREATE TABLE task_groups (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    name VARCHAR(100) NOT NULL,
    color VARCHAR(20) NOT NULL,
    icon VARCHAR(10),
    order_index INTEGER DEFAULT 0,
    time_zones JSONB DEFAULT '[]',
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Tasks
CREATE TABLE tasks (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    group_id UUID REFERENCES task_groups(id) ON DELETE SET NULL,
    title VARCHAR(500) NOT NULL,
    description TEXT DEFAULT '',
    status VARCHAR(20) DEFAULT 'pending',
    priority VARCHAR(10) DEFAULT 'medium',
    scheduled_at TIMESTAMPTZ,
    due_at TIMESTAMPTZ,
    completed_at TIMESTAMPTZ,
    assigned_to UUID[],
    attachments JSONB DEFAULT '[]',
    external_id TEXT,
    external_source VARCHAR(50),
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Task comments
CREATE TABLE task_comments (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    task_id UUID REFERENCES tasks(id) ON DELETE CASCADE,
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    content TEXT NOT NULL,
    is_ai BOOLEAN DEFAULT false,
    created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Goals
CREATE TABLE goals (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    title VARCHAR(500) NOT NULL,
    target_date TIMESTAMPTZ,
    progress_pct INTEGER DEFAULT 0,
    group_id UUID REFERENCES task_groups(id) ON DELETE SET NULL,
    plan JSONB DEFAULT '{}',
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Connectors
CREATE TABLE connectors (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    type VARCHAR(50) NOT NULL,
    config JSONB DEFAULT '{}',
    enabled BOOLEAN DEFAULT true,
    last_sync_at TIMESTAMPTZ,
    sync_log JSONB DEFAULT '[]',
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Indexes
CREATE INDEX idx_tasks_user ON tasks(user_id);
CREATE INDEX idx_tasks_group ON tasks(group_id);
CREATE INDEX idx_tasks_status ON tasks(status);
CREATE INDEX idx_tasks_scheduled ON tasks(scheduled_at);
CREATE INDEX idx_task_groups_user ON task_groups(user_id);
CREATE INDEX idx_task_comments_task ON task_comments(task_id);
CREATE INDEX idx_goals_user ON goals(user_id);

-- Insert default groups
INSERT INTO task_groups (id, user_id, name, color, icon, order_index, time_zones) VALUES
    (uuid_generate_v4(), NULL, 'Prace', '#3B82F6', 'B', 0, '[]'),
    (uuid_generate_v4(), NULL, 'Nakup', '#10B981', 'S', 1, '[]'),
    (uuid_generate_v4(), NULL, 'Study', '#8B5CF6', 'L', 2, '[]'),
    (uuid_generate_v4(), NULL, 'Plany', '#F59E0B', 'M', 3, '[]'),
    (uuid_generate_v4(), NULL, 'Sport', '#F97316', 'R', 4, '[]'),
    (uuid_generate_v4(), NULL, 'Duchovni', '#D4A017', 'D', 5, '[]'),
    (uuid_generate_v4(), NULL, 'Domaci', '#92400E', 'H', 6, '[]'),
    (uuid_generate_v4(), NULL, 'Relax', '#06B6D4', 'X', 7, '[]');
