version: "3"

vars:
  PROJECT: nalu_b2b
  BUILD_DIR: ./bin

  REMOTE_USER: root
  REMOTE_HOST: dc02-de
  EMAIL_SMTP_PORT: 1025
  EMAIL_SMTP_HOST: localhost

  LOCAL_DB_SERVICE: db
  LOCAL_DB_USER: nalu
  LOCAL_DB_NAME: nalu
  LOCAL_DB_PASSWORD: Maal12345678
  DUMP_FILE_NAME: 
    sh: echo nalu_$(date +%Y_%m_%d__%H_%M_%S).sql
  REMOTE_DB_SERVICE: "www_naluconcept_com_nalu_db"
  REMOTE_DB_NAME: "presta"
  REMOTE_DB_USER: "presta"
  REMOTE_DB_PASS: "presta"

tasks:

  reset:
    desc: Drop and recreate database
    cmds:
      - > 
        docker compose -p {{.PROJECT}} exec -T {{.LOCAL_DB_SERVICE}} mariadb -u {{.LOCAL_DB_USER}} --password={{.LOCAL_DB_PASSWORD}} 
        -e 'DROP DATABASE IF EXISTS {{.LOCAL_DB_NAME}}; CREATE DATABASE IF NOT EXISTS {{.LOCAL_DB_NAME}};'


  restore:
    desc: Restore DB from file
    aliases: [r]
    silent: true
    preconditions:
      - sh: '[ -n "{{index .CLI_ARGS_LIST 0}}" ]'
        msg: "Usage: task db:restore -- dump.sql"
    cmds:
      - task db:reset
      - |
        {{if eq (len .CLI_ARGS_LIST) 0}}
          echo "Error: No file path provided."
          exit 1
        {{else}}
          cat {{index .CLI_ARGS_LIST 0}} | docker compose -p {{.PROJECT}} exec -T {{.LOCAL_DB_SERVICE}} mariadb \
            -u {{.LOCAL_DB_USER}} --password={{.LOCAL_DB_PASSWORD}} \
            {{.LOCAL_DB_NAME}}
        {{end}}
      - task db:mal


  migrations_apply_localy:
    aliases: [mal]
    desc: Apply Migrations to local database
    silent: true
    cmds:
      - | 
        sed '/-- +goose Down/,$d' i18n/migrations/20260302163100_routes.sql | docker compose -p {{.PROJECT}} exec -T {{.LOCAL_DB_SERVICE}} mariadb -u {{.LOCAL_DB_USER}} --password={{.LOCAL_DB_PASSWORD}} {{.LOCAL_DB_NAME}}        
        sed '/-- +goose Down/,$d' i18n/migrations/20260302163122_create_tables.sql | docker compose -p {{.PROJECT}} exec -T {{.LOCAL_DB_SERVICE}} mariadb -u {{.LOCAL_DB_USER}} --password={{.LOCAL_DB_PASSWORD}} {{.LOCAL_DB_NAME}}
        sed '/-- +goose Down/,$d' i18n/migrations/20260302163123_create_tables_data.sql | docker compose -p {{.PROJECT}} exec -T {{.LOCAL_DB_SERVICE}} mariadb -u {{.LOCAL_DB_USER}} --password={{.LOCAL_DB_PASSWORD}} {{.LOCAL_DB_NAME}} 
        sed '/-- +goose Down/,$d' i18n/migrations/20260302163152_translations_backoffice.sql | docker compose -p {{.PROJECT}} exec -T {{.LOCAL_DB_SERVICE}} mariadb -u {{.LOCAL_DB_USER}} --password={{.LOCAL_DB_PASSWORD}} {{.LOCAL_DB_NAME}}
        sed '/-- +goose Down/,$d' i18n/migrations/20260302163157_translations_backend.sql | docker compose -p {{.PROJECT}} exec -T {{.LOCAL_DB_SERVICE}} mariadb -u {{.LOCAL_DB_USER}} --password={{.LOCAL_DB_PASSWORD}} {{.LOCAL_DB_NAME}} 
        sed '/-- +goose Down/,$d' i18n/migrations/20260319163200_procedures.sql | docker compose -p {{.PROJECT}} exec -T {{.LOCAL_DB_SERVICE}} mariadb -u {{.LOCAL_DB_USER}} --password={{.LOCAL_DB_PASSWORD}} {{.LOCAL_DB_NAME}} 

        