name: Initial Setup

inputs:
    php-version:
        type: string
        default: ""
    operating-system:
        required: true
        type: string

runs:
    using: "composite"
    steps:
        # 1. Setup PHP
        -   name: Setup PHP
            uses: shivammathur/setup-php@v2
            with:
                php-version: ${{ inputs.php-version }}
                extensions: curl, dom, gd, libxml, mbstring, zip, mysql, xml, intl, bcmath, redis-phpredis/phpredis@6.0.1
                ini-values: error_reporting=E_ALL
                coverage: pcov
                tools: composer:v2
            env:
                REDIS_CONFIGURE_OPTS: --enable-redis

        # 2. Composer Dependencies
        -   name: Get Composer cache directory
            id: composer-cache
            run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
            shell: bash

        -   name: Restore Composer cache
            uses: actions/cache/restore@v4
            with:
                path: ${{ steps.composer-cache.outputs.dir }}
                key: unit3d-${{ inputs.operating-system }}-php${{ inputs.php-version }}-composer-${{ hashFiles('**/composer.lock') }}
                restore-keys: |
                    unit3d-${{ inputs.operating-system }}-php${{ inputs.php-version }}-composer-

        -   name: Install Composer dependencies
            env:
                COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}
            run: composer install --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --optimize-autoloader
            shell: bash

        -   name: Save Composer cache
            uses: actions/cache/save@v4
            with:
                path: ${{ steps.composer-cache.outputs.dir }}
                key: unit3d-${{ inputs.operating-system }}-php${{ inputs.php-version }}-composer-${{ hashFiles('**/composer.lock') }}

        # 3. Bun Dependencies
        -   name: Setup Bun
            uses: oven-sh/setup-bun@v2
            with:
                bun-version: latest

        -   name: Restore Bun cache
            uses: actions/cache/restore@v4
            with:
                path: ~/.bun/install/cache
                key: unit3d-${{ inputs.operating-system }}-php${{ inputs.php-version }}-bun-${{ hashFiles('**/bun.lockb') }}
                restore-keys: |
                    unit3d-${{ inputs.operating-system }}-php${{ inputs.php-version }}-bun-

        -   name: Install Bun dependencies
            run: bun ci
            shell: bash

        -   name: Compile assets
            run: bun run build
            shell: bash

        -   name: Save Bun cache
            uses: actions/cache/save@v4
            with:
                path: ~/.bun/install/cache
                key: unit3d-${{ inputs.operating-system }}-php${{ inputs.php-version }}-bun-${{ hashFiles('**/bun.lockb') }}

        # 4. Prepare Application
        -   name: Prepare Laravel environment
            run: cp .env.example .env
            shell: bash

        -   name: Generate application key
            run: php artisan key:generate
            shell: bash

        -   name: Clear application cache
            run: php artisan clear:all_cache
            shell: bash
