package auctiora;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Test auction parsing logic using saved HTML from test.html
 * Tests the markup data extraction for each auction found
 */
@Slf4j
public class AuctionParsingTest {
   
   private static String testHtml;
   
   @BeforeAll
   public static void loadTestHtml() throws IOException {
      // Load the test HTML file
      testHtml = Files.readString(Paths.get("src/test/resources/test_auctions.html"));
      log.info("Loaded test HTML ({} characters)", testHtml.length());
   }
   
   @Test
   public void testLocationPatternMatching() {
      log.info("\n=== Location Pattern Tests ===");
      
      // Test different location formats
      var testCases = new String[]{
              "<p>Amsterdam, NL</p>",
              "<p class=\"flex truncate\"><span class=\"w-full truncate\">Sofia,<!-- --> </span>BG</p>",
              "<p>Berlin, DE</p>",
              "<span>Brussels,</span>BE"
      };
      
      for (var testHtml : testCases) {
         var doc  = Jsoup.parse(testHtml);
         var elem = doc.select("p, span").first();
         
         if (elem != null) {
            var text = elem.text();
            log.info("\nTest: {}", testHtml);
            log.info("Text: {}", text);
            
            // Test regex pattern
            if (text.matches(".*[A-Z]{2}$")) {
               var countryCode = text.substring(text.length() - 2);
               var cityPart    = text.substring(0, text.length() - 2).trim().replaceAll("[,\\s]+$", "");
               log.info("→ Extracted: {}, {}", cityPart, countryCode);
            } else {
               log.info("→ No match");
            }
         }
      }
   }
   
   @Test
   public void testFullTextPatternMatching() {
      log.info("\n=== Full Text Pattern Tests ===");
      
      // Test the complete auction text format
      var testCases = new String[]{
              "woensdag om 18:00 1 Vrachtwagens voor bedrijfsvoertuigen Loßburg, DE",
              "maandag om 14:30 5 Industriële machines Amsterdam, NL",
              "vrijdag om 10:00 12 Landbouwmachines Antwerpen, BE"
      };
      
      for (var testText : testCases) {
         log.info("\nParsing: \"{}\"", testText);
         
         // Simulated extraction
         var remaining = testText;
         
         // Extract time
         var timePattern = java.util.regex.Pattern.compile("(\\w+)\\s+om\\s+(\\d{1,2}:\\d{2})");
         var timeMatcher = timePattern.matcher(remaining);
         if (timeMatcher.find()) {
            log.info("  Time: {} om {}", timeMatcher.group(1), timeMatcher.group(2));
            remaining = remaining.substring(timeMatcher.end()).trim();
         }
         
         // Extract location
         var locPattern = java.util.regex.Pattern.compile(
                 "([A-ZÀ-ÿa-z][A-ZÀ-ÿa-z\\s\\-'öäüßàèéêëïôùûç]+?),\\s*([A-Z]{2})\\s*$"
                                                         );
         var locMatcher = locPattern.matcher(remaining);
         if (locMatcher.find()) {
            log.info("  Location: {}, {}", locMatcher.group(1), locMatcher.group(2));
            remaining = remaining.substring(0, locMatcher.start()).trim();
         }
         
         // Extract lot count
         var lotPattern = java.util.regex.Pattern.compile("^(\\d+)\\s+");
         var lotMatcher = lotPattern.matcher(remaining);
         if (lotMatcher.find()) {
            log.info("  Lot count: {}", lotMatcher.group(1));
            remaining = remaining.substring(lotMatcher.end()).trim();
         }
         
         // What remains is title
         log.info("  Title: {}", remaining);
      }
   }
}
