stages:
  - test
  - build
  - deploy

variables:
  FLUTTER_VERSION: "3.16.0"
  ANDROID_SDK_VERSION: "34"
  GRADLE_OPTS: "-Dorg.gradle.daemon=false"

# Кэш для оптимизации сборки
cache:
  key: flutter-cache
  paths:
    - .pub-cache/
    - android/.gradle/
    - build/

# Тестирование
test:
  stage: test
  image: cirrusci/flutter:${FLUTTER_VERSION}
  before_script:
    - flutter --version
    - flutter pub get
  script:
    - flutter analyze
    - flutter test
  artifacts:
    reports:
      junit: test-results.xml
    paths:
      - coverage/
    expire_in: 1 week

# Сборка Android APK
build_android:
  stage: build
  image: cirrusci/flutter:${FLUTTER_VERSION}
  before_script:
    - flutter --version
    - flutter pub get
  script:
    - flutter build apk --release
    - flutter build appbundle --release
  artifacts:
    paths:
      - build/app/outputs/flutter-apk/app-release.apk
      - build/app/outputs/bundle/release/app-release.aab
    expire_in: 1 month
  rules:
    - if: '$CI_COMMIT_BRANCH'
    - if: '$CI_COMMIT_TAG'

# Сборка Linux приложения
build_linux:
  stage: build
  image: ubuntu:22.04
  before_script:
    - apt-get update -y
    - apt-get install -y curl git unzip xz-utils zip libglu1-mesa
    - apt-get install -y clang cmake ninja-build pkg-config libgtk-3-dev
    - apt-get install -y libblkid-dev liblzma-dev
    # Установка Flutter
    - git clone https://github.com/flutter/flutter.git -b stable --depth 1
    - export PATH="$PATH:`pwd`/flutter/bin"
    - flutter --version
    - flutter config --enable-linux-desktop
    - flutter pub get
  script:
    - flutter build linux --release
  artifacts:
    paths:
      - build/linux/x64/release/bundle/
    expire_in: 1 month
  rules:
    - if: '$CI_COMMIT_BRANCH'
    - if: '$CI_COMMIT_TAG'

# Деплой в Google Play (опционально)
deploy_android:
  stage: deploy
  image: ruby:3.0
  before_script:
    - gem install fastlane
  script:
    - cd android
    - fastlane supply --aab ../build/app/outputs/bundle/release/app-release.aab
  dependencies:
    - build_android
  rules:
    - if: '$CI_COMMIT_TAG'
      when: manual

# Деплой Linux приложения в GitLab Package Registry
deploy_linux:
  stage: deploy
  image: ubuntu:22.04
  before_script:
    - apt-get update -y
    - apt-get install -y curl zip
  script:
    - cd build/linux/x64/release/bundle
    - zip -r ../../../../../${CI_PROJECT_NAME}-linux-${CI_COMMIT_TAG}.zip .
    - cd ../../../../../
    - |
      curl --header "JOB-TOKEN: $CI_JOB_TOKEN" \
           --upload-file ${CI_PROJECT_NAME}-linux-${CI_COMMIT_TAG}.zip \
           "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}/${CI_COMMIT_TAG}/${CI_PROJECT_NAME}-linux-${CI_COMMIT_TAG}.zip"
  dependencies:
    - build_linux
  rules:
    - if: '$CI_COMMIT_TAG'
      when: manual

# Релиз на GitLab
release:
  stage: deploy
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  script:
    - |
      release-cli create \
        --name "Release $CI_COMMIT_TAG" \
        --tag-name $CI_COMMIT_TAG \
        --description "Release $CI_COMMIT_TAG" \
        --assets-link "{\"name\":\"Android APK\",\"url\":\"${CI_PROJECT_URL}/-/jobs/artifacts/$CI_COMMIT_TAG/download?job=build_android\"}" \
        --assets-link "{\"name\":\"Linux App\",\"url\":\"${CI_PROJECT_URL}/-/jobs/artifacts/$CI_COMMIT_TAG/download?job=build_linux\"}"
  dependencies:
    - build_android
    - build_linux
  rules:
    - if: '$CI_COMMIT_TAG'
      when: manual