# GitLab CI/CD Pipeline for Tarot Card Reader
# Runs tests on MRs and creates releases on main branch merges

stages:
  - test
  - build
  - release

variables:
  CARGO_HOME: $CI_PROJECT_DIR/.cargo
  RUST_VERSION: "1.78.0"

# Cache Rust dependencies
cache:
  key: "cargo-cache"
  paths:
    - .cargo/
    - target/

# Test stage - runs on all branches and MRs
test:
  stage: test
  image: rust:${RUST_VERSION}
  before_script:
    - rustc --version
    - cargo --version
  script:
    - echo "Running cargo check..."
    - cargo check --verbose
    - echo "Running cargo test..."
    - cargo test --verbose
    - echo "Running cargo clippy..."
    - rustup component add clippy
    - cargo clippy -- -D warnings
    - echo "Running cargo fmt check..."
    - rustup component add rustfmt
    - cargo fmt -- --check
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    - if: $CI_COMMIT_TAG

# Build stage - runs after tests pass
build:linux:
  stage: build
  image: rust:${RUST_VERSION}
  before_script:
    - rustc --version
    - cargo --version
  script:
    - echo "Building Linux release binary..."
    - cargo build --release --verbose
    - echo "Binary size:"
    - ls -lh target/release/tarot
    - echo "Testing release binary..."
    - ./target/release/tarot --version
    - ./target/release/tarot --help
    # Create artifacts directory
    - mkdir -p artifacts
    - cp target/release/tarot artifacts/tarot-linux-x86_64
    - cp README.md artifacts/
    - cp Cargo.toml artifacts/
  artifacts:
    name: "tarot-${CI_COMMIT_TAG}"
    paths:
      - artifacts/
    expire_in: 1 week
  rules:
    - if: $CI_COMMIT_TAG
      when: always

build:windows:
  stage: build
  image: rust:${RUST_VERSION}
  before_script:
    - rustc --version
    - cargo --version
    - rustup target add x86_64-pc-windows-gnu
    - apt-get update && apt-get install -y mingw-w64
  script:
    - echo "Building Windows release binary..."
    - cargo build --release --target x86_64-pc-windows-gnu --verbose
    - echo "Binary size:"
    - ls -lh target/x86_64-pc-windows-gnu/release/tarot.exe
    # Create artifacts directory
    - mkdir -p artifacts
    - cp target/x86_64-pc-windows-gnu/release/tarot.exe artifacts/tarot-windows-x86_64.exe
  artifacts:
    name: "tarot-${CI_COMMIT_TAG}-windows"
    paths:
      - artifacts/tarot-windows-x86_64.exe
    expire_in: 1 week
  rules:
    - if: $CI_COMMIT_TAG
      when: always

# Release stage - creates GitLab release on main branch
create_release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  dependencies:
    - build:linux
    - build:windows
  before_script:
    - apk add --no-cache curl jq gettext
  script:
    - echo "Creating release for tag ${CI_COMMIT_TAG}"
    # Extract version from tag (remove 'v' prefix if present)
    - export VERSION=${CI_COMMIT_TAG#v}
    - echo "Version:" ${VERSION}
    - echo "${VERSION}" > version.txt
    - export BUILD_DATE=$(date -u +"%Y-%m-%d %H:%M:%S UTC")
    # Create release notes from template
    - |
      cat > release_notes_template.md << 'EOF'
      # 🔮 Tarot Card Reader v${VERSION}
      
      ## 📦 Release Information
      - **Tag**: ${CI_COMMIT_TAG}
      - **Commit**: ${CI_COMMIT_SHORT_SHA}
      - **Branch**: ${CI_COMMIT_BRANCH}
      - **Build Date**: ${BUILD_DATE}
      
      ## 🚀 Features
      - Complete 78-card tarot deck support
      - Beautiful terminal output with colors
      - JSON export functionality
      - Comprehensive test suite
      - Command-line help and version flags
      
      ## 📊 Build Stats
      - Rust Version: ${RUST_VERSION}
      - Tests Passed: ✅
      - Clippy Checks: ✅
      - Format Checks: ✅
      
      ## 💾 Download
      Download the pre-built binary from the artifacts section below.
      
      ## 🎯 Usage
      ```bash
      # Run a tarot reading
      ./tarot
      
      # Get JSON output
      ./tarot --json
      
      # Show help
      ./tarot --help
      ```
      
      ## 🔗 Links
      - [Repository](${CI_PROJECT_URL})
      - [Pipeline](${CI_PIPELINE_URL})
      - [Commit](${CI_PROJECT_URL}/-/commit/${CI_COMMIT_SHA})
      EOF
    - envsubst < release_notes_template.md > release_notes.md
    - echo "Uploading Linux binary to GitLab Package Registry..."
    - |
      curl --header "JOB-TOKEN: $CI_JOB_TOKEN" \
        --upload-file artifacts/tarot-linux-x86_64 \
        "$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/tarot/${CI_COMMIT_TAG}/tarot-linux-x86_64"
    - echo "Linux binary uploaded successfully!"
    - echo "Uploading Windows binary to GitLab Package Registry..."
    - |
      curl --header "JOB-TOKEN: $CI_JOB_TOKEN" \
        --upload-file artifacts/tarot-windows-x86_64.exe \
        "$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/tarot/${CI_COMMIT_TAG}/tarot-windows-x86_64.exe"
    - echo "Windows binary uploaded successfully!"
  release:
    name: "Tarot Release ${CI_COMMIT_TAG}"
    tag_name: "${CI_COMMIT_TAG}"
    description: release_notes.md
    assets:
      links:
        - name: "tarot-linux-x86_64"
          url: "$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/tarot/${CI_COMMIT_TAG}/tarot-linux-x86_64"
          link_type: "package"
        - name: "tarot-windows-x86_64.exe"
          url: "$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/tarot/${CI_COMMIT_TAG}/tarot-windows-x86_64.exe"
          link_type: "package"
  rules:
    - if: $CI_COMMIT_TAG
      when: always

# Summary job
pipeline_summary:
  stage: .post
  image: alpine:latest
  script:
    - echo "Pipeline completed successfully!"
    - echo "Commit:" ${CI_COMMIT_SHORT_SHA}
    - echo "Branch:" ${CI_COMMIT_BRANCH}
    - echo "Pipeline URL:" ${CI_PIPELINE_URL}
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: always