CREATE TABLE IF NOT EXISTS YclientsTokens (
    AccountID VARCHAR(30) PRIMARY KEY,
    SalonID INT PRIMARY KEY,                 -- ID компании
    AccessToken TEXT NOT NULL DEFAULT '',
    Active BOOLEAN NOT NULL DEFAULT FALSE,
    Expiration BOOLEAN NOT NULL DEFAULT FALSE, -- флаг истек ли токен
    CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

create UNIQUE INDEX idx_unique_tokens_yclients ON YclientsTokens (SalonID, AccountID) WHERE Active = true and Expiration = false;

CREATE TABLE IF NOT EXISTS YclientsAccounts (
    ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY,
    AccountID VARCHAR(30) NOT NULL DEFAULT '', -- ID аккаунта у нас
    SalonID INT NOT NULL,                 -- ID компании
    Title text NOT NULL DEFAULT '',
    ShortDecription text NOT NULL DEFAULT '',
    Country VARCHAR(50) NOT NULL DEFAULT '',
    Deleted BOOLEAN NOT NULL DEFAULT FALSE,
    CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS YclientsAccountUsers (
    ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY,
    SalonID INT NOT NULL,                 -- ID компании
    YclientsID INT NOT NULL,                    -- ID пользователя в Yclients
    Name VARCHAR(512) NOT NULL DEFAULT '',
    Specialization text NOT NULL DEFAULT '',
    IDPosition int not null default 0,
    TitlePosition text not null default 0,
    Fired BOOLEAN not null default false, -- Уволен ли сотрудник
    Status BOOLEAN not null default false, -- Удален ли сотрудник
    Hidden BOOLEAN not null default false, -- Скрыт ли сотрудник для онлайн-записи
    YclientsUserID INT NOT NULL,                    -- ID пользователя в Yclients2
    Deleted BOOLEAN NOT NULL DEFAULT FALSE,
    CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CREATE TABLE IF NOT EXISTS YclientsCompany (
--     ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY,
--     SalonID INT NOT NULL,                 -- ID компании
--     Title text NOT NULL,
--     ShortDecription text NOT NULL,
--     Active INT NOT NULL,
--     Country text NOT NULL,
--     GroupPriority INT NOT NULL,
--     Deleted BOOLEAN NOT NULL DEFAULT FALSE,
--     CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
-- );

CREATE TABLE IF NOT EXISTS YclientsServices (
    ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY,
    SalonID INT NOT NULL,                 -- ID компании
    ServiceID INT NOT NULL,
    SalonServiceID INT NOT NULL,
    Title text NOT NULL,
    CategoryID INT NOT NULL,
    PriceMin text NOT NULL,
    PriceMax text NOT NULL,
    Discount text NOT NULL,
    Comment text NOT NULL,
    Active BOOLEAN not null default false,
    ApiID text NOT NULL,
    Staff JSONB NOT NULL DEFAULT '{}',
    Deleted BOOLEAN NOT NULL DEFAULT FALSE,
    CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS YclientsTimeSlots (
    ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY,
    SalonID INT NOT NULL,                 -- ID компании
    IsEnabled BOOLEAN NOT NULL DEFAULT false,
    WeekdaysSettings JSONB NOT NULL DEFAULT '[]',
    DatesSettings JSONB NOT NULL DEFAULT '{}',
    Deleted BOOLEAN NOT NULL DEFAULT FALSE,
    CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS YclientsRules (
    ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY,
    SalonID INT NOT NULL,                 -- ID компании
    QuizID INT NOT NULL, -- ID квиза на которое вешается правило
    Services JSONB NOT NULL DEFAULT '{}',
    FieldsRule JSONB NOT NULL DEFAULT '{}',
    CustomColor text NOT NULL Default '',
    Deleted BOOLEAN NOT NULL DEFAULT FALSE,
    CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE UNIQUE INDEX idx_unique_yclients_rules ON YclientsRules (SalonID, QuizID) WHERE Deleted = false;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_enum
        WHERE enumlabel = 'yclients_staff_id' AND enumtypid = 'question_type'::regtype
    ) THEN
ALTER TYPE question_type ADD VALUE 'yclients_staff_id';
END IF;
END $$;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_enum
        WHERE enumlabel = 'yclients_services' AND enumtypid = 'question_type'::regtype
    ) THEN
ALTER TYPE question_type ADD VALUE 'yclients_services';
END IF;
END $$;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_enum
        WHERE enumlabel = 'yclients_comment' AND enumtypid = 'question_type'::regtype
    ) THEN
ALTER TYPE question_type ADD VALUE 'yclients_comment';
END IF;
END $$;