stages:
  - test
  - build
  - deploy

variables:
  FLUTTER_VERSION: "stable"
  # Optimize for RAM usage
  FLUTTER_BUILD_FLAGS: "--split-debug-info=./debug-symbols --obfuscate --dart-define=dart.vm.profile=false"
  PUB_CACHE: "${CI_PROJECT_DIR}/.pub-cache"

cache:
  paths:
    - .pub-cache/

# Test stage - runs first to catch issues early
test:dart:
  stage: test
  image: ghcr.io/cirruslabs/flutter:${FLUTTER_VERSION}
  script:
    - flutter --version
    - flutter pub get
    - flutter analyze --fatal-warnings
    - flutter test --coverage
    - flutter build web --release --dart-define=dart.vm.profile=false
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura.xml
    paths:
      - coverage/
      - build/web/
    expire_in: 7 days
  rules:
    - if: $CI_MERGE_REQUEST_IID
    - if: $CI_COMMIT_BRANCH
    - if: $CI_COMMIT_TAG

build:apk:arm64:
  stage: build
  image: ghcr.io/cirruslabs/flutter:${FLUTTER_VERSION}
  script:
    - flutter pub get
    - mkdir -p debug-symbols
    - flutter build apk --release --target-platform android-arm64 --split-per-abi ${FLUTTER_BUILD_FLAGS}
  artifacts:
    paths:
      - build/app/outputs/flutter-apk/app-arm64-v8a-release.apk
    expire_in: 30 days
  rules:
    - if: $CI_COMMIT_TAG
      when: always
    - if: $CI_COMMIT_BRANCH =~ /^dev|main/
      when: on_success

build:apk:arm:
  stage: build
  image: ghcr.io/cirruslabs/flutter:${FLUTTER_VERSION}
  before_script:
    # Update version from tag if present
    - |
      if [ -n "$CI_COMMIT_TAG" ]; then
        VERSION_NAME="${CI_COMMIT_TAG#v}"
        BUILD_NUMBER=$(echo $CI_COMMIT_TAG | sed 's/[^0-9]//g')
        echo "Updating version to $VERSION_NAME+$BUILD_NUMBER"
        sed -i "s/^version: .*/version: $VERSION_NAME+$BUILD_NUMBER/" pubspec.yaml
      fi
  script:
    - flutter pub get
    - mkdir -p debug-symbols
    - flutter build apk --release --target-platform android-arm --split-per-abi ${FLUTTER_BUILD_FLAGS}
  artifacts:
    paths:
      - build/app/outputs/flutter-apk/app-armeabi-v7a-release.apk
    expire_in: 30 days
  rules:
    - if: $CI_COMMIT_TAG
      when: always
    - if: $CI_COMMIT_BRANCH =~ /^dev|main/
      when: on_success

build:apk:x64:
  stage: build
  image: ghcr.io/cirruslabs/flutter:${FLUTTER_VERSION}
  script:
    - flutter pub get
    - mkdir -p debug-symbols
    - flutter build apk --release --target-platform android-x64 --split-per-abi ${FLUTTER_BUILD_FLAGS}
  artifacts:
    paths:
      - build/app/outputs/flutter-apk/app-x86_64-release.apk
    expire_in: 30 days
  rules:
    - if: $CI_COMMIT_TAG
      when: always
    - if: $CI_COMMIT_BRANCH =~ /^dev|main/
      when: on_success

deploy:release:
  stage: deploy
  image: alpine:latest
  needs:
    - build:apk:arm64
    - build:apk:arm
    - build:apk:x64
  before_script:
    - apk add --no-cache curl jq coreutils
  script:
    - |
      if [ -n "$CI_COMMIT_TAG" ]; then
        VERSION="$CI_COMMIT_TAG"
      else
        VERSION="v0.0.${CI_PIPELINE_ID}"
      fi
      
      echo "Creating GitLab Release: $VERSION"
      echo "Commit: ${CI_COMMIT_SHORT_SHA}"
      echo "Branch: ${CI_COMMIT_BRANCH}"
      
      APK_ARM64="build/app/outputs/flutter-apk/app-arm64-v8a-release.apk"
      APK_ARM32="build/app/outputs/flutter-apk/app-armeabi-v7a-release.apk"
      APK_X86="build/app/outputs/flutter-apk/app-x86_64-release.apk"
      
      RELEASE_DESCRIPTION="## NeoMovies Mobile ${VERSION}

      **Build Info:**
      - Commit: \`${CI_COMMIT_SHORT_SHA}\`
      - Branch: \`${CI_COMMIT_BRANCH}\`
      - Pipeline: [#${CI_PIPELINE_ID}](${CI_PIPELINE_URL})

      **Downloads:**"
      
      FILE_COUNT=0
      
      if [ -f "$APK_ARM64" ]; then
        FILE_COUNT=$((FILE_COUNT+1))
        SIZE_ARM64=$(du -h "$APK_ARM64" | cut -f1)
        RELEASE_DESCRIPTION="${RELEASE_DESCRIPTION}\n- ARM64 (arm64-v8a): \`app-arm64-v8a-release.apk\` (${SIZE_ARM64}) - Recommended for modern devices"
      fi
      
      if [ -f "$APK_ARM32" ]; then
        FILE_COUNT=$((FILE_COUNT+1))
        SIZE_ARM32=$(du -h "$APK_ARM32" | cut -f1)
        RELEASE_DESCRIPTION="${RELEASE_DESCRIPTION}\n- ARM32 (armeabi-v7a): \`app-armeabi-v7a-release.apk\` (${SIZE_ARM32}) - For older devices"
      fi
      
      if [ -f "$APK_X86" ]; then
        FILE_COUNT=$((FILE_COUNT+1))
        SIZE_X86=$(du -h "$APK_X86" | cut -f1)
        RELEASE_DESCRIPTION="${RELEASE_DESCRIPTION}\n- x86_64: \`app-x86_64-release.apk\` (${SIZE_X86}) - For emulators"
      fi
      
      if [ $FILE_COUNT -eq 0 ]; then
        echo "No release artifacts found!"
        exit 1
      fi
      
      echo "Found $FILE_COUNT artifact(s) to release"
      
      RELEASE_DATA=$(jq -n \
        --arg name "NeoMovies ${VERSION}" \
        --arg tag "${VERSION}" \
        --arg desc "$RELEASE_DESCRIPTION" \
        --arg ref "${CI_COMMIT_SHA}" \
        '{name: $name, tag_name: $tag, description: $desc, ref: $ref}')
      
      echo "Creating release via GitLab API..."
      
      curl --fail-with-body -s -X POST \
        "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/releases" \
        --header "PRIVATE-TOKEN: ${CI_JOB_TOKEN}" \
        --header "Content-Type: application/json" \
        --data "$RELEASE_DATA" || \
      curl -s -X PUT \
        "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/releases/${VERSION}" \
        --header "PRIVATE-TOKEN: ${CI_JOB_TOKEN}" \
        --header "Content-Type: application/json" \
        --data "$RELEASE_DATA"
      
      echo ""
      echo "Uploading APK files to Package Registry..."
      
      if [ -f "$APK_ARM64" ]; then
        echo "Uploading app-arm64-v8a-release.apk..."
        curl --fail -s --request PUT \
          --header "PRIVATE-TOKEN: ${CI_JOB_TOKEN}" \
          --upload-file "$APK_ARM64" \
          "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/neomovies/${VERSION}/app-arm64-v8a-release.apk"
        
        LINK_DATA=$(jq -n \
          --arg name "app-arm64-v8a-release.apk" \
          --arg url "${CI_PROJECT_URL}/-/package_files/${CI_PROJECT_ID}/packages/generic/neomovies/${VERSION}/app-arm64-v8a-release.apk" \
          --arg type "package" \
          '{name: $name, url: $url, link_type: $type}')
        
        curl -s --request POST \
          --header "PRIVATE-TOKEN: ${CI_JOB_TOKEN}" \
          --header "Content-Type: application/json" \
          --data "$LINK_DATA" \
          "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/releases/${VERSION}/assets/links"
        
        echo "ARM64 APK uploaded"
      fi
      
      if [ -f "$APK_ARM32" ]; then
        echo "Uploading app-armeabi-v7a-release.apk..."
        curl --fail -s --request PUT \
          --header "PRIVATE-TOKEN: ${CI_JOB_TOKEN}" \
          --upload-file "$APK_ARM32" \
          "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/neomovies/${VERSION}/app-armeabi-v7a-release.apk"
        
        LINK_DATA=$(jq -n \
          --arg name "app-armeabi-v7a-release.apk" \
          --arg url "${CI_PROJECT_URL}/-/package_files/${CI_PROJECT_ID}/packages/generic/neomovies/${VERSION}/app-armeabi-v7a-release.apk" \
          --arg type "package" \
          '{name: $name, url: $url, link_type: $type}')
        
        curl -s --request POST \
          --header "PRIVATE-TOKEN: ${CI_JOB_TOKEN}" \
          --header "Content-Type: application/json" \
          --data "$LINK_DATA" \
          "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/releases/${VERSION}/assets/links"
        
        echo "ARM32 APK uploaded"
      fi
      
      if [ -f "$APK_X86" ]; then
        echo "Uploading app-x86_64-release.apk..."
        curl --fail -s --request PUT \
          --header "PRIVATE-TOKEN: ${CI_JOB_TOKEN}" \
          --upload-file "$APK_X86" \
          "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/neomovies/${VERSION}/app-x86_64-release.apk"
        
        LINK_DATA=$(jq -n \
          --arg name "app-x86_64-release.apk" \
          --arg url "${CI_PROJECT_URL}/-/package_files/${CI_PROJECT_ID}/packages/generic/neomovies/${VERSION}/app-x86_64-release.apk" \
          --arg type "package" \
          '{name: $name, url: $url, link_type: $type}')
        
        curl -s --request POST \
          --header "PRIVATE-TOKEN: ${CI_JOB_TOKEN}" \
          --header "Content-Type: application/json" \
          --data "$LINK_DATA" \
          "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/releases/${VERSION}/assets/links"
        
        echo "x86_64 APK uploaded"
      fi
      
      echo ""
      echo "================================================"
      echo "Release created successfully!"
      echo "View release: ${CI_PROJECT_URL}/-/releases/${VERSION}"
      echo "Pipeline artifacts: ${CI_JOB_URL}/artifacts/browse"
      echo "================================================"
  artifacts:
    paths:
      - build/app/outputs/flutter-apk/*.apk
    expire_in: 90 days
  rules:
    - if: $CI_COMMIT_TAG
      when: always
    - if: $CI_COMMIT_BRANCH =~ /^dev|main/
      when: on_success