-- =============================================
-- SP Deployment Script for Commission Calculation
-- Version: 1.0
-- Created: 2025-12-19
-- =============================================

/*
    این اسکریپت برای نصب Stored Procedure های محاسبه کمیسیون استفاده میشود.
    
    قبل از اجرا:
    1. مطمئن شوید که به دیتابیس CMS متصل هستید
    2. کاربر شما دسترسی CREATE PROCEDURE داشته باشد
    
    ترتیب اجرا:
    1. sp_CalculateWeeklyBalances.sql
    2. sp_CalculateWeeklyCommissionPool.sql
    
    یا این فایل را اجرا کنید که هر دو SP را نصب میکند.
*/

PRINT '========================================='
PRINT 'Commission Calculation SP Deployment'
PRINT '========================================='
PRINT ''

-- =============================================
-- Check Schema
-- =============================================
IF NOT EXISTS (SELECT 1 FROM sys.schemas WHERE name = 'CMS')
BEGIN
    PRINT 'ERROR: Schema CMS does not exist!'
    RETURN
END

PRINT 'Schema CMS: OK'
PRINT ''

-- =============================================
-- Drop existing procedures if exist
-- =============================================
IF EXISTS (SELECT 1 FROM sys.procedures WHERE name = 'sp_CalculateWeeklyBalances' AND schema_id = SCHEMA_ID('CMS'))
BEGIN
    DROP PROCEDURE CMS.sp_CalculateWeeklyBalances;
    PRINT 'Dropped existing sp_CalculateWeeklyBalances'
END

IF EXISTS (SELECT 1 FROM sys.procedures WHERE name = 'sp_CalculateWeeklyCommissionPool' AND schema_id = SCHEMA_ID('CMS'))
BEGIN
    DROP PROCEDURE CMS.sp_CalculateWeeklyCommissionPool;
    PRINT 'Dropped existing sp_CalculateWeeklyCommissionPool'
END

PRINT ''
PRINT 'Creating Stored Procedures...'
PRINT ''

-- =============================================
-- SP 1: sp_CalculateWeeklyBalances
-- =============================================
GO
-- (content of sp_CalculateWeeklyBalances.sql here)
-- Run sp_CalculateWeeklyBalances.sql separately

GO
-- (content of sp_CalculateWeeklyCommissionPool.sql here)  
-- Run sp_CalculateWeeklyCommissionPool.sql separately

GO

PRINT ''
PRINT '========================================='
PRINT 'Deployment Complete!'
PRINT '========================================='
PRINT ''
PRINT 'How to use:'
PRINT ''
PRINT '-- 1. Set calculation strategy in SystemConfigurations:'
PRINT 'UPDATE CMS.SystemConfigurations '
PRINT 'SET Value = ''SP'' '
PRINT 'WHERE [Key] = ''Commission.CalculationStrategy'''
PRINT ''
PRINT '-- Or for ORM mode:'
PRINT 'UPDATE CMS.SystemConfigurations '
PRINT 'SET Value = ''ORM'' '
PRINT 'WHERE [Key] = ''Commission.CalculationStrategy'''
PRINT ''
PRINT '-- 2. Manual execution (optional):'
PRINT 'DECLARE @RowCount INT, @PoolId BIGINT'
PRINT 'EXEC CMS.sp_CalculateWeeklyBalances @WeekDefinitionId = 1, @ForceRecalculate = 0, @RowCount = @RowCount OUTPUT'
PRINT 'PRINT ''Rows affected: '' + CAST(@RowCount AS VARCHAR)'
PRINT ''
PRINT 'EXEC CMS.sp_CalculateWeeklyCommissionPool @WeekDefinitionId = 1, @ForceRecalculate = 0, @PoolId = @PoolId OUTPUT'
PRINT 'PRINT ''Pool ID: '' + CAST(@PoolId AS VARCHAR)'
GO
