-- Tabela de soluções disponíveis (CRM, ERP, etc.)
CREATE TABLE IF NOT EXISTS solutions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name VARCHAR(100) NOT NULL UNIQUE,
    slug VARCHAR(50) NOT NULL UNIQUE,
    icon VARCHAR(50),
    description TEXT,
    is_active BOOLEAN DEFAULT true,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Relacionamento N:N entre planos e soluções
CREATE TABLE IF NOT EXISTS plan_solutions (
    plan_id UUID REFERENCES plans(id) ON DELETE CASCADE,
    solution_id UUID REFERENCES solutions(id) ON DELETE CASCADE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (plan_id, solution_id)
);

-- Índices
CREATE INDEX idx_solutions_slug ON solutions(slug);
CREATE INDEX idx_solutions_is_active ON solutions(is_active);
CREATE INDEX idx_plan_solutions_plan_id ON plan_solutions(plan_id);
CREATE INDEX idx_plan_solutions_solution_id ON plan_solutions(solution_id);

-- Seed inicial: CRM
INSERT INTO solutions (id, name, slug, icon, description, is_active) VALUES
    ('00000000-0000-0000-0000-000000000001', 'CRM', 'crm', 'users', 'Gestão de clientes e relacionamento', true)
ON CONFLICT (slug) DO NOTHING;
