name: Generate Skin previews, OSK files and per skin documentation

on:
  push:
    paths:
      - '.gitea/workflows/*'
      - 'Skins/**/*'
  workflow_dispatch:
    inputs:
      force_rebuild:
        description: 'Force rebuild all skins'
        required: false
        default: 'false'
      target_skins:
        description: 'Comma-separated list of skin folder names to rebuild (e.g., "Skin1,Skin2")'
        required: false
        default: ''

env:
  DANSER_DIR: "/app/danser"
  DANSER_VIDEO_DIR: "/app/danser/videos"
  DANSER_SCREENSHOT_DIR: "/app/danser/screenshots"
  SKINS_DIR: "${{ github.workspace }}/Skins"
  DANSER_SKINS_DIR: "/app/danser/skins"
  DEFAULT_SKIN_DIR: "/app/danser/skins/default-skin"
  REPO_SCREENSHOT_DIR: "${{ github.workspace }}/media/gameplay"
  REPO_MOD_ICONS_DIR: "${{ github.workspace }}/media/icons"
  REPO_RANKING_PANEL_DIR: "${{ github.workspace }}/media/panel"
  REPO_THUMBNAIL_DIR: "${{ github.workspace }}/media/thumbnail"
  README_PATH: "${{ github.workspace }}/README.md"
  GAMEPLAY_REPLAY_PATH: "/app/danser/custom-replays/yomi_yori.osr"
  THUMBNAIL_REPLAY_PATH: "/app/danser/custom-replays/combo_colors.osr"
  PANEL_REPLAY_PATH: "/app/danser/custom-replays/2000_gekis.osr"
  OSK_PATH: "${{ github.workspace }}/export"
  IMAGE_NAME: osc/skins-image
  REGISTRY_URL: "https://${{ vars.CONTAINER_REGISTRY }}"
  OSU_ID: ${{ vars.OSUID }}
  DOC_DIR: "${{ github.workspace }}/docs"

permissions:
  contents: write

jobs:
  generate_everything:
    name: Full CI/CD Pipeline
    runs-on: danser
    container:
      image: ${{ vars.CONTAINER_REGISTRY }}/${{ env.IMAGE_NAME }}:latest
      options: >-
        --gpus all
        --env NVIDIA_DRIVER_CAPABILITIES=all
        --env NVIDIA_VISIBLE_DEVICES=all
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN  }}

      - name: Discover and Detect Skins
        id: discover
        uses: ${{ env.REGISTRY_URL}}/osc/reusable-actions/.gitea/actions/discover-skins@main
        with:
          force_rebuild: ${{ github.event.inputs.force_rebuild }}
          target_skins: ${{ github.event.inputs.target_skins }}

      - name: Pull Git LFS
        uses: ${{ env.REGISTRY_URL}}/osc/reusable-actions/.gitea/actions/pull-lfs@main
        with:
          changed_skins_file: ${{ steps.discover.outputs.changed_skins_file }}

      - name: Prepare Assets
        id: prepare
        uses: ${{ env.REGISTRY_URL}}/osc/reusable-actions/.gitea/actions/prepare-assets@main
        with:
          all_skins: ${{ steps.discover.outputs.all_skins }}

      - name: Create Tag
        id: tag
        uses: ${{ env.REGISTRY_URL}}/osc/reusable-actions/.gitea/actions/create-tag@main

      - name: Generate Previews
        uses: ${{ env.REGISTRY_URL}}/osc/reusable-actions/.gitea/actions/generate-previews@main
        with:
          changed_skins_file: ${{ steps.discover.outputs.changed_skins_file }}

      - name: Generate Mod Icons and Convert Images
        uses: ${{ env.REGISTRY_URL}}/osc/reusable-actions/.gitea/actions/generate-icons@main
        with:
          changed_skins_file: ${{ steps.discover.outputs.changed_skins_file }}

      - name: Generate Documentation
        uses: ${{ env.REGISTRY_URL}}/osc/reusable-actions/.gitea/actions/generate-docs@main
        with:
          new_tag: ${{ steps.tag.outputs.new_tag }}
          readme_path: ${{ env.README_PATH }}
          doc_dir: ${{ env.DOC_DIR }}
          user_repository: ${{ steps.prepare.outputs.user_repository }}

      - name: Cleanup Files
        uses: ${{ env.REGISTRY_URL}}/osc/reusable-actions/.gitea/actions/cleanup@main
        with:
          all_skins: ${{ steps.discover.outputs.all_skins }}

      - name: Commit and Push
        uses: ${{ env.REGISTRY_URL}}/osc/reusable-actions/.gitea/actions/git-commit-push@main
        with:
          new_tag: ${{ steps.tag.outputs.new_tag }}
