-- =========================
-- TEAMS
-- =========================
CREATE TABLE teams (
    id BIGSERIAL PRIMARY KEY,
    external_id VARCHAR(100) UNIQUE,
    name VARCHAR(255) NOT NULL,
    short_name VARCHAR(100),
    logo_url TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_teams_external_id ON teams(external_id);


-- =========================
-- PLAYERS
-- =========================
CREATE TABLE players (
    id BIGSERIAL PRIMARY KEY,
    external_id VARCHAR(100) UNIQUE,
    team_id BIGINT NOT NULL REFERENCES teams(id) ON DELETE CASCADE,
    full_name VARCHAR(255) NOT NULL,
    first_name VARCHAR(100),
    last_name VARCHAR(100),
    number VARCHAR(20),
    position VARCHAR(50),
    birth_date DATE,
    photo TEXT,
    photo_enabled BOOLEAN NOT NULL DEFAULT FALSE,
    video TEXT,
    height_cm INTEGER,
    weight_kg INTEGER,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_players_team_id ON players(team_id);
CREATE INDEX idx_players_external_id ON players(external_id);


-- =========================
-- MATCHES (расписание)
-- =========================
CREATE TABLE matches (
    id BIGSERIAL PRIMARY KEY,
    external_id VARCHAR(100) UNIQUE,
    home_team_id BIGINT NOT NULL REFERENCES teams(id),
    away_team_id BIGINT NOT NULL REFERENCES teams(id),
    match_date TIMESTAMP,
    status VARCHAR(50) NOT NULL DEFAULT 'scheduled',
    home_score INTEGER,
    away_score INTEGER,
    tour VARCHAR(100),
    season VARCHAR(50),
    source_key VARCHAR(50),
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_matches_external_id ON matches(external_id);
CREATE INDEX idx_matches_season ON matches(season);
CREATE INDEX idx_matches_source_key ON matches(source_key);
CREATE INDEX idx_matches_date ON matches(match_date);


-- =========================
-- STANDINGS (таблица турнира)
-- =========================
CREATE TABLE standings (
    id BIGSERIAL PRIMARY KEY,
    team_id BIGINT NOT NULL REFERENCES teams(id) ON DELETE CASCADE,
    season VARCHAR(50) NOT NULL,
    played INTEGER NOT NULL DEFAULT 0,
    wins INTEGER NOT NULL DEFAULT 0,
    losses INTEGER NOT NULL DEFAULT 0,
    draws INTEGER NOT NULL DEFAULT 0,
    points_for INTEGER NOT NULL DEFAULT 0,
    points_against INTEGER NOT NULL DEFAULT 0,
    points INTEGER NOT NULL DEFAULT 0,
    position INTEGER,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
    UNIQUE(team_id, season)
);

CREATE INDEX idx_standings_season ON standings(season);

-- =========================
-- PROJECT SETTINGS / PARSER SOURCES
-- =========================
CREATE TABLE IF NOT EXISTS app_settings (
    key VARCHAR(100) PRIMARY KEY,
    value TEXT NOT NULL DEFAULT '',
    updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS parser_sources (
    key VARCHAR(50) PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    tournament_id VARCHAR(100),
    round_id VARCHAR(100),
    season VARCHAR(50),
    calendar_type VARCHAR(50) NOT NULL DEFAULT 'tours',
    logo_base_path TEXT,
    photo_base_path TEXT,
    teams_url TEXT,
    schedule_url TEXT,
    standings_url TEXT,
    match_base_url TEXT,
    base_url TEXT NOT NULL DEFAULT 'https://wfl.rfs.ru',
    sort_order INTEGER NOT NULL DEFAULT 100,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

INSERT INTO app_settings (key, value, updated_at)
VALUES
    ('default_parser_source_key', 'SUPERLEAGUE', NOW()),
    ('rfs_base_url', 'https://wfl.rfs.ru', NOW())
ON CONFLICT (key) DO NOTHING;

INSERT INTO parser_sources (
    key, title, tournament_id, round_id, season, calendar_type, logo_base_path, photo_base_path,
    teams_url, schedule_url, standings_url, match_base_url, base_url,
    sort_order, is_active, created_at, updated_at
)
VALUES
    (
        'SUPERLEAGUE',
        'Суперлига 2026',
        '1061879',
        '1117550',
        '2025/2026',
        'tours',
        'D:\Графика\ФУТБОЛ\Женская Суперлига 2026\Teams Logos',
        'D:\Графика\ФУТБОЛ\Женская Суперлига 2026\Photo',
        'https://wfl.rfs.ru/tournament/1061879/teams',
        'https://wfl.rfs.ru/tournament/1061879/calendar?round_id=1117550&type=tours',
        'https://wfl.rfs.ru/tournament/1061879/tables',
        'https://wfl.rfs.ru/match/',
        'https://wfl.rfs.ru',
        10,
        TRUE,
        NOW(),
        NOW()
    ),
    (
        'RUSSIAN_CUP',
        'Кубок России 2026',
        '1064908',
        '1125159',
        '2026',
        'stages',
        'D:\Графика\ФУТБОЛ\Кубок России 2026\Teams Logos',
        'D:\Графика\ФУТБОЛ\Кубок России 2026\Photo',
        'https://wfl.rfs.ru/tournament/1064908/teams',
        'https://wfl.rfs.ru/tournament/1064908/calendar?round_id=1125159&type=stages',
        'https://wfl.rfs.ru/tournament/1064908/tables',
        'https://wfl.rfs.ru/match/',
        'https://wfl.rfs.ru',
        20,
        TRUE,
        NOW(),
        NOW()
    )
ON CONFLICT (key) DO NOTHING;
