# frozen_string_literal: true

namespace :symfony do
  desc 'Install importmap vendor assets'
  task :importmap_install do
    on roles(:app) do
      symfony_console('importmap:install')
    end
  end

  desc 'Build Tailwind CSS (minified)'
  task :tailwind_build do
    on roles(:app) do
      within release_path do
        # OVH /tmp has noexec — Tailwind/Bun needs a writable+executable tmpdir
        execute :env, "TMPDIR=#{fetch(:tmp_dir)}", :php, fetch(:symfony_console_path), 'tailwind:build', '--minify'
      end
    end
  end

  desc 'Compile asset map'
  task :asset_map_compile do
    on roles(:app) do
      symfony_console('asset-map:compile')
    end
  end

  desc 'Run database migrations'
  task :migrate do
    on roles(:db) do
      symfony_console('doctrine:migrations:migrate', '--no-interaction --allow-no-migration')
    end
  end

  desc 'Clear and warm up cache'
  task :cache_clear do
    on roles(:app) do
      symfony_console('cache:clear', '--env=prod')
    end
  end
end

after 'deploy:updated', 'symfony:importmap_install'
after 'symfony:importmap_install', 'symfony:tailwind_build'
after 'symfony:tailwind_build', 'symfony:asset_map_compile'
after 'symfony:asset_map_compile', 'symfony:migrate'
after 'symfony:migrate', 'symfony:cache_clear'
