image: rust:1.93-slim

variables:
  CARGO_HOME: $CI_PROJECT_DIR/.cargo
  RUSTFLAGS: -D warnings
  GIT_DEPTH: 1
  RUST_BACKTRACE: 1

stages:
  - build
  - test
  - quality
  - docs
  - security
  - release

# Cache dependencies for faster builds
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .cargo/
    - target/

# Multi-platform build jobs
build:linux-x86_64:
  stage: build
  image: rust:1.93-slim
  variables:
    CARGO_TARGET: x86_64-unknown-linux-gnu
    PLATFORM: linux-x86_64
  script:
    - rustup target add $CARGO_TARGET
    - apt-get update && apt-get install -y libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev ripgrep fd-find
    - cargo build --release --target $CARGO_TARGET
    - chmod +x target/$CARGO_TARGET/release/fishez
    - mkdir -p dist
    - cp target/$CARGO_TARGET/release/fishez dist/fishez-$PLATFORM
    - chmod +x dist/fishez-$PLATFORM
    - ls -lh dist/
  artifacts:
    paths:
      - dist/fishez-$PLATFORM
    expire_in: 30 days

build:linux-aarch64:
  stage: build
  image: rust:1.93-slim
  variables:
    CARGO_TARGET: aarch64-unknown-linux-gnu
    PLATFORM: linux-aarch64
  script:
    - rustup target add $CARGO_TARGET
    - apt-get update && apt-get install -y libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev ripgrep fd-find
    - cargo build --release --target $CARGO_TARGET
    - chmod +x target/$CARGO_TARGET/release/fishez
    - mkdir -p dist
    - cp target/$CARGO_TARGET/release/fishez dist/fishez-$PLATFORM
    - chmod +x dist/fishez-$PLATFORM
    - ls -lh dist/
  artifacts:
    paths:
      - dist/fishez-$PLATFORM
    expire_in: 30 days

build:macos-x86_64:
  stage: build
  image: rust:1.93-slim
  variables:
    CARGO_TARGET: x86_64-apple-darwin
    PLATFORM: macos-x86_64
  script:
    - rustup target add $CARGO_TARGET
    - cargo build --release --target $CARGO_TARGET
    - chmod +x target/$CARGO_TARGET/release/fishez
    - mkdir -p dist
    - cp target/$CARGO_TARGET/release/fishez dist/fishez-$PLATFORM
    - chmod +x dist/fishez-$PLATFORM
    - ls -lh dist/
  artifacts:
    paths:
      - dist/fishez-$PLATFORM
    expire_in: 30 days

build:macos-aarch64:
  stage: build
  image: rust:1.93-slim
  variables:
    CARGO_TARGET: aarch64-apple-darwin
    PLATFORM: macos-aarch64
  script:
    - rustup target add $CARGO_TARGET
    - cargo build --release --target $CARGO_TARGET
    - chmod +x target/$CARGO_TARGET/release/fishez
    - mkdir -p dist
    - cp target/$CARGO_TARGET/release/fishez dist/fishez-$PLATFORM
    - chmod +x dist/fishez-$PLATFORM
    - ls -lh dist/
  artifacts:
    paths:
      - dist/fishez-$PLATFORM
    expire_in: 30 days

# Run tests on all platforms
test:unit:
  stage: test
  script:
    - cargo test --lib
  artifacts:
    reports:
      junit: target/junit.xml
    paths:
      - target/junit.xml

test:integration:
  stage: test
  script:
    - cargo test --test integration_flows
    - cargo install cargo-tarpaulin --quiet
    - cargo tarpaulin --out Xml --output-dir . --test integration_flows --out JUnit
  dependencies:
    - test:unit
  artifacts:
    reports:
      junit: tarpaulin.xml
    paths:
      - tarpaulin.xml

# Clippy linting
clippy:
  stage: quality
  script:
    - cargo clippy --all-targets --all-features -- -D warnings
  allow_failure: false

# Rustfmt check
fmt:
  stage: quality
  script:
    - cargo fmt -- --check
  allow_failure: false

# Dependency tree validation
tree-check:
  stage: quality
  script:
    - cargo tree
  allow_failure: false

# Code security scan (dependency audit)
security:audit:
  stage: security
  script:
    - cargo install cargo-audit --quiet
    - cargo audit
  allow_failure: true

# Build project
build:debug:
  stage: build
  script:
    - cargo build
  allow_failure: false

build:release:
  stage: build
  script:
    - cargo build --release
  artifacts:
    paths:
      - target/release/fishez
    expire_in: 1 week
  allow_failure: false

# Documentation
docs:build:
  stage: docs
  script:
    - cargo doc --no-deps --document-private-items
  artifacts:
    paths:
      - target/doc/
    expire_in: 1 week
  allow_failure: false

docs:check:
  stage: docs
  script:
    - cargo doc --no-deps --document-private-items --quiet
  allow_failure: false

# Code coverage report
coverage:
  stage: test
  script:
    - cargo install cargo-tarpaulin --quiet
    - cargo tarpaulin --out Html --output-dir coverage --exclude-files "src/main.rs" --exclude-files "tests/*" --timeout 300
  coverage: '/Total\s+lines\s+([\d.]+)/'
  artifacts:
    paths:
      - coverage/
    expire_in: 30 days
  allow_failure: true

# Security scan using grype (vulnerability scanner)
security:grype:
  stage: security
  image: anchore/grype:latest
  script:
    - grype cargo:target/debug/fishez --fail-on high
  allow_failure: true

# Binary size analysis
analyze:size:
  stage: quality
  script:
    - ls -lh target/release/fishez
  allow_failure: false

# Verify docs build without warnings
docs:warning-check:
  stage: docs
  script:
    - cargo doc --no-deps --document-private-items 2>&1 | grep -i warning || true
  allow_failure: true

# Release pipeline (manual trigger)
release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  script:
    - echo "Preparing release..."
    - mkdir -p release-artifacts
    - cp dist/fishez-* release-artifacts/
  rules:
    - if: '$CI_COMMIT_TAG'
  artifacts:
    paths:
      - release-artifacts/
  release:
    tag_name: '$CI_COMMIT_TAG'
    name: 'fishez-$CI_COMMIT_TAG'
    description: 'Release of fishez version $CI_COMMIT_TAG'
    assets:
      links:
        - name: 'fishez-linux-x86_64'
          url: 'https://gitlab.com/ioma8/fishez/-/jobs/$CI_JOB_ID/artifacts/file/dist/fishez-linux-x86_64'
          link_type: 'other'
        - name: 'fishez-linux-aarch64'
          url: 'https://gitlab.com/ioma8/fishez/-/jobs/$CI_JOB_ID/artifacts/file/dist/fishez-linux-aarch64'
          link_type: 'other'
        - name: 'fishez-macos-x86_64'
          url: 'https://gitlab.com/ioma8/fishez/-/jobs/$CI_JOB_ID/artifacts/file/dist/fishez-macos-x86_64'
          link_type: 'other'
        - name: 'fishez-macos-aarch64'
          url: 'https://gitlab.com/ioma8/fishez/-/jobs/$CI_JOB_ID/artifacts/file/dist/fishez-macos-aarch64'
          link_type: 'other'

# Run all quality checks in one job (for CI/CD pipeline)
quality:all:
  stage: quality
  script:
    - cargo fmt -- --check
    - cargo clippy --all-targets --all-features -- -D warnings
    - cargo tree
    - cargo test
  dependencies: []
  allow_failure: false
  when: manual
  allow_manual: true
