#!/bin/zsh

# Quick script to generate simple data tasks like creating/deleting abacus profiles,
# updating database records or schema, anything that is a repetitive and simple SQL/neo4j queries,
# or creating files that need naming conventions.
set -e

load_env() {
  declare -r env_filepath="./scripts/.env"
  if [[ ! -f "$env_filepath" ]]; then
    echo "Error: $env_filepath file not found" >&2
    exit 1
  fi

  source "$env_filepath"

  if [[ -z "$DEV_USERNAME" ]] || [[ -z "$DEV_IDENTITY_ID" ]]; then
    echo "Error: DEV_USERNAME and DEV_IDENTITY_ID must be set in $env_filepath" >&2
    exit 1
  fi
}

show_help() {
  cat << EOF
Usage: ./scripts/generate.sh <TASK> [...args]

A script to generate simple data tasks like creating/deleting abacus profiles,
updating database records or schema, or creating files with naming conventions.

All commands read DEV_USERNAME (and where noted DEV_IDENTITY_ID) from scripts/.env.
If arguments are omitted, missing values will be prompted for interactively.

Available Tasks:
  create-abacus-profiles <user_ids> <ticket_num>
      Create Abacus profiles to grant internal users access.
      <user_ids>    Comma-separated identity UUIDs
      <ticket_num>  Ticket identifier (e.g. ACC-1234)
      Requires: DEV_USERNAME, DEV_IDENTITY_ID
      Output: neo4j/neo4j-orchard/build/changelog/dml/<ticket_num>-create-abacus-profile.cypher
      Note: Non-Sony users may require email unmasking after generation.

  delete-abacus-profiles <user_ids> <ticket_num>
      Remove Abacus access for one or more users by their profile IDs.
      <user_ids>    Comma-separated profile IDs
      <ticket_num>  Ticket identifier (e.g. ACC-1234)
      Requires: DEV_USERNAME
      Output: neo4j/neo4j-orchard/build/changelog/dml/<ticket_num>_delete-abacus-profiles.xml

  make-abacus-profiles-readonly <user_ids> <ticket_num>
      Set Abacus profiles to read-only by their profile UUIDs.
      <user_ids>    Comma-separated profile UUIDs
      <ticket_num>  Ticket identifier (e.g. ACC-1234)
      Requires: DEV_USERNAME, DEV_IDENTITY_ID
      Output: neo4j/neo4j-orchard/build/changelog/dml/<ticket_num>-make-abacus-profiles-readonly.cypher

  create-dml-file <ticket_num> <task_name>
      Create a blank Liquibase SQL file for data changes (DML).
      <ticket_num>  Ticket identifier (e.g. ACC-1234)
      <task_name>   Snake_case description of the change (e.g. update_signing_entity)
      Requires: DEV_USERNAME
      Output: royalty_accounting/build/changelog/dml/<ticket_num>_<task_name>.sql

  create-ddl-file <ticket_num> <task_name>
      Create a blank Liquibase SQL file for schema changes (DDL) and register it in the master changelog.
      <ticket_num>  Ticket identifier (e.g. ACC-1234)
      <task_name>   Snake_case description of the change (e.g. add_column_to_contract)
      Requires: DEV_USERNAME
      Output: royalty_accounting/build/changelog/ddl/<ticket_num>_<task_name>.sql
      Also updates: royalty_accounting/build/db.changelog-master.xml

  hard-delete-accounts <account_ids> <ticket_num>
      Permanently delete accounts and all associated data. Backs up data to a rollback table first.
      <account_ids> Comma-separated account IDs
      <ticket_num>  Ticket identifier (e.g. ACC-1234)
      Requires: DEV_USERNAME
      Output: royalty_accounting/build/changelog/dml/<ticket_num>_hard_delete_accounts.sql
      Warning: Destructive and irreversible. Intended for accounts created in error only.

  hard-delete-contracts <contract_ids> <ticket_num>
      Permanently delete contracts and all associated data. Backs up data to a rollback table first.
      <contract_ids> Comma-separated contract IDs
      <ticket_num>   Ticket identifier (e.g. ACC-1234)
      Requires: DEV_USERNAME
      Output: royalty_accounting/build/changelog/dml/<ticket_num>_hard_delete_contracts.sql
      Warning: Destructive and irreversible. Intended for contracts created in error only.

  resync-contract-vat-details <contract_ids> <ticket_num>
      Generate a Kafka changelog XML to trigger resyncing of VAT details for contracts.
      <contract_ids> Comma-separated contract IDs
      <ticket_num>   Ticket identifier (e.g. ACC-1234)
      Requires: DEV_USERNAME
      Output: kafka-db-deploy/kafka/build/changelog/dml/<ticket_num>_resync_vat_details.xml

  bulk-add-upcs <json_file>
      Generate SQL to bulk add UPC lists to product contract terms.
      <json_file>   Path to JSON input file (see scripts/examples/BULK_UPC_README.md)
      Requires: DEV_USERNAME, DEV_IDENTITY_ID
      Output: royalty_accounting/build/changelog/dml/<ticket_id>-bulk-add-upcs.sql

Options:
  -h, --help    Show this help message

Examples:
  ./scripts/generate.sh create-abacus-profiles "uuid-1111,uuid-2222" ACC-1234
  ./scripts/generate.sh delete-abacus-profiles "12345,67890" ACC-1234
  ./scripts/generate.sh make-abacus-profiles-readonly "uuid-aaaa,uuid-bbbb" ACC-1234
  ./scripts/generate.sh create-dml-file ACC-1234 update_signing_entity_for_contract
  ./scripts/generate.sh create-ddl-file ACC-1234 add_column_to_contract
  ./scripts/generate.sh hard-delete-accounts "785115,785116" ACC-1234
  ./scripts/generate.sh hard-delete-contracts "532783,532784" ACC-1234
  ./scripts/generate.sh resync-contract-vat-details "532783,532784,532785" ACC-1234
  ./scripts/generate.sh bulk-add-upcs scripts/examples/ACC-9468.json

EOF
}

run_task() {
  local task=$1
  local arg1=$2
  local arg2=$3
  local dev_username=$DEV_USERNAME
  local dev_identity_id=$DEV_IDENTITY_ID

  case $task in
    create-abacus-profiles)
      if [[ -z "$arg1" ]] || [[ -z "$arg2" ]]; then
        echo "Error: create-abacus-profiles requires <input_file> <schema>" >&2
        exit 1
      fi
      zsh ./scripts/generators/create-abacus-profiles.sh "$arg1" "$arg2" "$dev_username" "$dev_identity_id"
      ;;

    delete-abacus-profiles)
      if [[ -z "$arg1" ]] || [[ -z "$arg2" ]]; then
        echo "Error: delete-abacus-profiles requires <input_file> <schema>" >&2
        exit 1
      fi
      zsh ./scripts/generators/delete-abacus-profiles.sh "$arg1" "$arg2" "$dev_username"
      ;;

    create-ddl-file)
      if [[ -z "$arg1" ]] || [[ -z "$arg2" ]]; then
        echo "Error: create-ddl-file requires <schema> <ticket_id>" >&2
        exit 1
      fi
      zsh ./scripts/generators/create-ddl-file.sh "$arg1" "$arg2" "$dev_username"
      ;;

    create-dml-file)
      if [[ -z "$arg1" ]] || [[ -z "$arg2" ]]; then
        echo "Error: create-dml-file requires <schema> <ticket_id>" >&2
        exit 1
      fi
      zsh ./scripts/generators/create-dml-file.sh "$arg1" "$arg2" "$dev_username"
      ;;

    hard-delete-accounts)
      if [[ -z "$arg1" ]] || [[ -z "$arg2" ]]; then
        echo "Error: hard-delete-accounts requires <input_file> <schema>" >&2
        exit 1
      fi
      zsh ./scripts/generators/hard-delete-accounts.sh "$arg1" "$arg2" "$dev_username"
      ;;

    hard-delete-contracts)
      if [[ -z "$arg1" ]] || [[ -z "$arg2" ]]; then
        echo "Error: hard-delete-contracts requires <input_file> <schema>" >&2
        exit 1
      fi
      zsh ./scripts/generators/hard-delete-contracts.sh "$arg1" "$arg2" "$dev_username"
      ;;

    make-abacus-profiles-readonly)
      if [[ -z "$arg1" ]] || [[ -z "$arg2" ]]; then
        echo "Error: make-abacus-profiles-readonly requires <input_file> <schema>" >&2
        exit 1
      fi
      zsh ./scripts/generators/make-abacus-profiles-readonly.sh "$arg1" "$arg2" "$dev_username" "$dev_identity_id"
      ;;

    bulk-add-upcs)
      if [[ -z "$arg1" ]]; then
        echo "Error: bulk-add-upcs requires <json_file>" >&2
        exit 1
      fi
      if [[ ! -f "$arg1" ]]; then
        echo "Error: File not found: $arg1" >&2
        exit 1
      fi
      python3 ./scripts/generators/bulk-add-upc-lists-to-product-terms.py -i "$arg1" --dev-username "$dev_username" --dev-identity-id "$dev_identity_id"
      ;;

    resync-contract-vat-details)
      if [[ -z "$arg1" ]] || [[ -z "$arg2" ]]; then
        echo "Error: resync-contract-vat-details requires <input_file> <schema>" >&2
        exit 1
      fi
      zsh ./scripts/generators/resync-contract-vat-details.sh "$arg1" "$arg2" "$dev_username"
      ;;

    "")
      echo "Error: No task specified" >&2
      echo "Run './scripts/generate.sh --help' for usage information" >&2
      exit 1
      ;;

    *)
      echo "Error: Unknown task '$task'" >&2
      echo "Run './scripts/generate.sh --help' for available tasks" >&2
      exit 1
      ;;
  esac
}

main() {
  if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
    show_help
    exit 0
  fi

  load_env

  run_task "$@"
}

main "$@"
