require 'spec_helper'

RSpec.describe 'Jekyll Drafts Integration' do
  let(:site_with_drafts) { create_test_site(show_drafts: true) }
  let(:site_without_drafts) { create_test_site(show_drafts: false) }
  let(:context_with_drafts) { create_liquid_context(site_with_drafts) }
  let(:context_without_drafts) { create_liquid_context(site_without_drafts) }

  after do
    [site_with_drafts, site_without_drafts].each do |site|
      FileUtils.rm_rf(site.source)
      FileUtils.rm_rf(site.dest)
      FileUtils.rm_rf(site.cache_dir)
    end
  end

  describe 'drafts with show_drafts enabled' do
    it 'processes assets when drafts contain asset references' do
      # Create assets that will be referenced in drafts
      create_test_image(site_with_drafts, 'assets/hero.jpg', 1200, 800)
      create_test_image(site_with_drafts, 'assets/logo.png', 200, 100)
      create_test_css(site_with_drafts, 'assets/styles.css')

      # Create a draft that references these assets
      draft_content = <<~MARKDOWN
        ---
        title: My Draft Post
        layout: post
        ---

        This is a draft post with various assets:

        ## Hero Image
        ![Hero]({% image_transform assets/hero.jpg[width="800"][format="webp"] %})

        ## Responsive Image
        <picture>
          <source type="image/webp" {% srcset assets/hero.jpg[format="webp"] 400 800 1200 %}>
          <img src="{% image_transform assets/hero.jpg[width="800"] %}" alt="Hero">
        </picture>

        ## Logo
        <img src="{% image_transform assets/logo.png[width="100"] %}" alt="Logo">

        ## Styles
        <link rel="stylesheet" href="{% asset assets/styles.css %}">
      MARKDOWN

      create_test_draft(site_with_drafts, 'my-draft.md', draft_content)

      # Process assets
      skyhook = Jekyll::Skyhook.instance(site_with_drafts)
      skyhook.process_assets

      # Verify assets are processed and available
      manifest_path = File.join(site_with_drafts.cache_dir, 'skyhook', 'assets-manifest.json')
      expect(File.exist?(manifest_path)).to be true

      manifest = JSON.parse(File.read(manifest_path))
      expect(manifest.keys).to include('assets/hero.jpg')
      expect(manifest.keys).to include('assets/logo.png')
      expect(manifest.keys).to include('assets/styles.css')

      # Test that asset tags work
      css_tag = create_tag(Jekyll::Tags::AssetTag, 'assets/styles.css')
      css_result = css_tag.render(context_with_drafts)
      expect(css_result).to start_with('/_digested/assets/styles-')

      # Test that image transformation tags work
      hero_tag = create_tag(Jekyll::Tags::ImageTransformTag, 'assets/hero.jpg[width="800"][format="webp"]')
      hero_result = hero_tag.render(context_with_drafts)
      expect(hero_result).to include('formatwebp-width800')
      expect(hero_result).to end_with('.webp')

      # Test that srcset tags work
      srcset_tag = create_tag(Jekyll::Tags::SrcsetTag, 'assets/hero.jpg[format="webp"] 400 800 1200')
      srcset_result = srcset_tag.render(context_with_drafts)
      expect(srcset_result).to include('srcset=')
      expect(srcset_result).to include('formatwebp-width400')
      expect(srcset_result).to include('formatwebp-width800')
      expect(srcset_result).to include('formatwebp-width1200')
    end

    it 'handles CSS url() rewriting in drafts context' do
      # Create assets
      create_test_image(site_with_drafts, 'assets/images/bg.jpg')

      css_content = <<~CSS
        .draft-header {
          background: url('images/bg.jpg') center/cover;
          height: 300px;
        }
      CSS
      create_test_css(site_with_drafts, 'assets/draft-styles.css', css_content)

      # Create draft
      create_test_draft(site_with_drafts, 'draft-with-css.md')

      # Process assets
      skyhook = Jekyll::Skyhook.instance(site_with_drafts)
      skyhook.process_assets

      # Check CSS URL rewriting worked
      css_tag = create_tag(Jekyll::Tags::AssetTag, 'assets/draft-styles.css')
      css_path = css_tag.render(context_with_drafts)
      full_css_path = File.join(site_with_drafts.source, css_path[1..-1])

      processed_css = File.read(full_css_path)
      expect(processed_css).to include('url(/_digested/assets/images/bg-')
      expect(processed_css).not_to include("url('images/bg.jpg')")
    end

    it 'creates proper static files that Jekyll can serve' do
      # Create assets
      create_test_image(site_with_drafts, 'assets/draft-hero.jpg')
      create_test_css(site_with_drafts, 'assets/draft-styles.css')

      # Create draft
      create_test_draft(site_with_drafts, 'static-files-test.md')

      # Process assets
      skyhook = Jekyll::Skyhook.instance(site_with_drafts)
      skyhook.process_assets

      # Check static files were registered
      asset_static_files = site_with_drafts.static_files.select do |file|
        file.name.match?(/.*-[a-f0-9]{32}\./)
      end

      expect(asset_static_files.length).to be >= 2
      expect(asset_static_files.any? { |f| f.name.include?('draft-hero-') }).to be true
      expect(asset_static_files.any? { |f| f.name.include?('draft-styles-') }).to be true
    end
  end

  describe 'drafts with show_drafts disabled (production)' do
    it 'still processes assets even when drafts are not shown' do
      # This simulates production where drafts aren't shown but assets should still work
      create_test_image(site_without_drafts, 'assets/prod-image.jpg')
      create_test_css(site_without_drafts, 'assets/prod-styles.css')

      # Create draft (won't be processed by Jekyll but assets should still work)
      create_test_draft(site_without_drafts, 'prod-draft.md')

      # Process assets
      skyhook = Jekyll::Skyhook.instance(site_without_drafts)
      skyhook.process_assets

      # Assets should be processed regardless of draft visibility
      manifest_path = File.join(site_without_drafts.cache_dir, 'skyhook', 'assets-manifest.json')
      expect(File.exist?(manifest_path)).to be true

      manifest = JSON.parse(File.read(manifest_path))
      expect(manifest.keys).to include('assets/prod-image.jpg')
      expect(manifest.keys).to include('assets/prod-styles.css')

      # Asset tags should work
      css_tag = create_tag(Jekyll::Tags::AssetTag, 'assets/prod-styles.css')
      css_result = css_tag.render(context_without_drafts)
      expect(css_result).to start_with('/_digested/assets/prod-styles-')
    end
  end

  describe 'cross-environment consistency' do
    it 'generates same asset digests regardless of draft visibility' do
      # Create identical assets in both environments
      css_content = 'body { color: blue; font-family: Arial; }'

      create_test_css(site_with_drafts, 'assets/consistent.css', css_content)
      create_test_css(site_without_drafts, 'assets/consistent.css', css_content)

      # Process in both environments
      skyhook_with_drafts = Jekyll::Skyhook.instance(site_with_drafts)
      skyhook_without_drafts = Jekyll::Skyhook.instance(site_without_drafts)

      skyhook_with_drafts.process_assets
      skyhook_without_drafts.process_assets

      # Get results from both
      tag_with_drafts = create_tag(Jekyll::Tags::AssetTag, 'assets/consistent.css')
      tag_without_drafts = create_tag(Jekyll::Tags::AssetTag, 'assets/consistent.css')

      result_with_drafts = tag_with_drafts.render(context_with_drafts)
      result_without_drafts = tag_without_drafts.render(context_without_drafts)

      # Should get same digest for same content
      digest_with_drafts = result_with_drafts.match(/-([a-f0-9]{32})\./)[1]
      digest_without_drafts = result_without_drafts.match(/-([a-f0-9]{32})\./)[1]

      expect(digest_with_drafts).to eq(digest_without_drafts)
    end
  end
end
