#!/bin/bash

# Default behaviors
M_ONLY=false
SEED_CLASS=""
SKIP_C=false
SKIP_M=false
SKIP_S=false
SKIP_P=false
SKIP_O=false
SKIP_SCR=false
SKIP_NI=false
SKIP_NCI=false
SKIP_NB=false

# Parse arguments
for arg in "$@"
do
    case $arg in
        --m-only)
        M_ONLY=true
        shift
        ;;
        --s-class=*)
        SEED_CLASS="${arg#*=}"
        shift
        ;;
        --skip-c)
        SKIP_C=true
        shift
        ;;
        --skip-m)
        SKIP_M=true
        shift
        ;;
        --skip-s)
        SKIP_S=true
        shift
        ;;
        --skip-p)
        SKIP_P=true
        shift
        ;;
        --skip-o)
        SKIP_O=true
        shift
        ;;
        --skip-scr)
        SKIP_SCR=true
        shift
        ;;
        --skip-ni)
        SKIP_NI=true
        shift
        ;;
        --skip-nci)
        SKIP_NCI=true
        shift
        ;;
        --skip-nb)
        SKIP_NB=true
        shift
        ;;
        --help)
        echo "Usage: ./deploy.sh [OPTIONS]"
        echo "Options:"
        echo "  --m-only           : Only migrate (skip fresh migration)"
        echo "  --s-class=ClassName: Specify the seeder class"
        echo "  --skip-c           : Skip composer install"
        echo "  --skip-m           : Skip migration commands"
        echo "  --skip-s           : Skip seeding"
        echo "  --skip-p           : Skip passport installation"
        echo "  --skip-o           : Skip optimize"
        echo "  --skip-scr         : Skip scribe generation"
        echo "  --skip-ni          : Skip npm install"
        echo "  --skip-nci         : Skip npm ci"
        echo "  --skip-nb          : Skip npm run build"
        exit 0
        ;;
    esac
done

# Conditional command executions

if [ "$SKIP_C" = false ]; then
    composer install
fi

if [ "$SKIP_M" = false ]; then
    if [ "$M_ONLY" = true ]; then
        php artisan migrate
    else
        php artisan migrate:fresh
    fi
fi

if [ "$SKIP_S" = false ]; then
    if [ -z "$SEED_CLASS" ]; then
        php artisan db:seed
    else
        php artisan db:seed --class=$SEED_CLASS
    fi
fi

if [ "$SKIP_P" = false ]; then
    php artisan passport:install
fi

if [ "$SKIP_O" = false ]; then
    php artisan optimize:clear
fi

if [ "$SKIP_SCR" = false ]; then
    php artisan scribe:generate
fi

if [ "$SKIP_NI" = false ]; then
    npm install
fi

if [ "$SKIP_NCI" = false ]; then
    npm ci
fi

if [ "$SKIP_NB" = false ]; then
    npm run build
fi