#!/bin/bash

# Script untuk switch customer risk level antara L1 dan L2
# Usage: ./switch-risk-level.sh [OPTIONS]

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Default values
DRY_RUN=false
CONFIRM=true

echo "======================================"
echo "  Customer Risk Level Switcher"
echo "======================================"
echo ""

# Function to display help
show_help() {
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  --from=L1|L2       Source risk level (required)"
    echo "  --to=L1|L2         Target risk level (required)"
    echo "  --customer-id=ID   Switch specific customer"
    echo "  --resort-id=ID     Filter by resort"
    echo "  --dry-run          Preview without applying changes"
    echo "  --no-confirm       Skip confirmation prompt"
    echo "  --help             Show this help message"
    echo ""
    echo "Examples:"
    echo "  # Switch all L1 customers to L2"
    echo "  $0 --from=L1 --to=L2"
    echo ""
    echo "  # Preview changes without applying"
    echo "  $0 --from=L1 --to=L2 --dry-run"
    echo ""
    echo "  # Switch specific customer"
    echo "  $0 --from=L1 --to=L2 --customer-id=abc-123-def"
    echo ""
    echo "  # Switch L2 to L1 for specific resort"
    echo "  $0 --from=L2 --to=L1 --resort-id=RESORT001"
    echo ""
}

# Parse arguments
FROM_LEVEL=""
TO_LEVEL=""
CUSTOMER_ID=""
RESORT_ID=""

while [[ $# -gt 0 ]]; do
    case $1 in
        --from=*)
            FROM_LEVEL="${1#*=}"
            shift
            ;;
        --to=*)
            TO_LEVEL="${1#*=}"
            shift
            ;;
        --customer-id=*)
            CUSTOMER_ID="${1#*=}"
            shift
            ;;
        --resort-id=*)
            RESORT_ID="${1#*=}"
            shift
            ;;
        --dry-run)
            DRY_RUN=true
            shift
            ;;
        --no-confirm)
            CONFIRM=false
            shift
            ;;
        --help)
            show_help
            exit 0
            ;;
        *)
            echo -e "${RED}Unknown option: $1${NC}"
            show_help
            exit 1
            ;;
    esac
done

# Validate required parameters
if [ -z "$FROM_LEVEL" ] || [ -z "$TO_LEVEL" ]; then
    echo -e "${RED}Error: --from and --to parameters are required${NC}"
    echo ""
    show_help
    exit 1
fi

# Validate risk levels
if [[ ! "$FROM_LEVEL" =~ ^(L1|L2)$ ]]; then
    echo -e "${RED}Error: --from must be L1 or L2${NC}"
    exit 1
fi

if [[ ! "$TO_LEVEL" =~ ^(L1|L2)$ ]]; then
    echo -e "${RED}Error: --to must be L1 or L2${NC}"
    exit 1
fi

if [ "$FROM_LEVEL" == "$TO_LEVEL" ]; then
    echo -e "${RED}Error: --from and --to must be different${NC}"
    exit 1
fi

# Build command
CMD="php artisan customer:switch-risk-level --from=$FROM_LEVEL --to=$TO_LEVEL"

if [ -n "$CUSTOMER_ID" ]; then
    CMD="$CMD --customer-id=$CUSTOMER_ID"
fi

if [ -n "$RESORT_ID" ]; then
    CMD="$CMD --resort-id=$RESORT_ID"
fi

if [ "$DRY_RUN" == true ]; then
    CMD="$CMD --dry-run"
fi

if [ "$CONFIRM" == false ]; then
    CMD="$CMD --no-interaction"
fi

# Display command info
echo -e "${YELLOW}Configuration:${NC}"
echo "  From: $FROM_LEVEL"
echo "  To:   $TO_LEVEL"
[ -n "$CUSTOMER_ID" ] && echo "  Customer ID: $CUSTOMER_ID"
[ -n "$RESORT_ID" ] && echo "  Resort ID: $RESORT_ID"
[ "$DRY_RUN" == true ] && echo -e "  ${YELLOW}Mode: DRY RUN (no changes will be applied)${NC}"
echo ""

# Run the command
echo -e "${GREEN}Running command...${NC}"
echo ""

cd "$(dirname "$0")" && $CMD

echo ""
echo -e "${GREEN}Done!${NC}"
