-- ============================================
-- TEMPLATE-SAAS: Portfolio Row Level Security
-- Version: 1.0.0
-- Module: SAAS-019
-- ============================================

-- ============================================
-- Enable RLS on all tables
-- ============================================
ALTER TABLE portfolio.categories ENABLE ROW LEVEL SECURITY;
ALTER TABLE portfolio.products ENABLE ROW LEVEL SECURITY;
ALTER TABLE portfolio.variants ENABLE ROW LEVEL SECURITY;
ALTER TABLE portfolio.prices ENABLE ROW LEVEL SECURITY;

-- ============================================
-- Categories Policies
-- ============================================
CREATE POLICY categories_tenant_isolation ON portfolio.categories
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

CREATE POLICY categories_insert ON portfolio.categories
    FOR INSERT
    WITH CHECK (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

CREATE POLICY categories_update ON portfolio.categories
    FOR UPDATE
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

CREATE POLICY categories_delete ON portfolio.categories
    FOR DELETE
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

-- ============================================
-- Products Policies
-- ============================================
CREATE POLICY products_tenant_isolation ON portfolio.products
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

CREATE POLICY products_insert ON portfolio.products
    FOR INSERT
    WITH CHECK (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

CREATE POLICY products_update ON portfolio.products
    FOR UPDATE
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

CREATE POLICY products_delete ON portfolio.products
    FOR DELETE
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

-- ============================================
-- Variants Policies
-- ============================================
CREATE POLICY variants_tenant_isolation ON portfolio.variants
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

CREATE POLICY variants_insert ON portfolio.variants
    FOR INSERT
    WITH CHECK (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

CREATE POLICY variants_update ON portfolio.variants
    FOR UPDATE
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

CREATE POLICY variants_delete ON portfolio.variants
    FOR DELETE
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

-- ============================================
-- Prices Policies
-- ============================================
CREATE POLICY prices_tenant_isolation ON portfolio.prices
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

CREATE POLICY prices_insert ON portfolio.prices
    FOR INSERT
    WITH CHECK (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

CREATE POLICY prices_update ON portfolio.prices
    FOR UPDATE
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

CREATE POLICY prices_delete ON portfolio.prices
    FOR DELETE
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);
