# Glamorgan Spring Bay Council — Public Notices scraper (site page, not PlanBuild)

require "nokogiri"
require_relative "../lib/http"
require_relative "../lib/util"
require_relative "../lib/scraper_helpers"

TABLE = ENV.fetch("TABLE_NAME")  # run_all.sh sets from filename: da_glamorgan
URL   = "https://gsbc.tas.gov.au/services-facilities/public-notices/"

DB.ensure_table!(TABLE)

# Try to extract a reference from visible text or file names
REF_RX = %r{(DA|PLA|APP|APPLICATION)\s*([0-9]{4})\s*/\s*([A-Za-z0-9\-_.]+)}i
def extract_reference(str)
  s = str.to_s
  if (m = s.match(REF_RX))
    return "#{m[1].upcase} #{m[2]} / #{m[3]}"
  end
  nil
end

html = Http.get(URL)
doc  = Nokogiri::HTML(html)

# Find a table that looks like the notices list
tables = doc.css("table")
table  = tables.find do |t|
  headers = t.css("thead th").map { |th| th.text.strip.downcase }
  headers.empty? ? t.css("tbody tr:first-child td").size >= 4 : headers.size >= 4
end

unless table
  puts "No notices table found on #{URL}"
  exit 0
end

rows = table.css("tbody tr")
puts "Found #{rows.length} rows for #{TABLE}"

saved = 0

rows.each_with_index do |row, idx|
  tds = row.css("td")
  next if tds.empty?

  # Expected columns on this page:
  # 0: Description, 1: Address, 2: Application date, 3: On notice to, 4: Document link
  description        = text_or(tds[0])
  address            = text_or(tds[1])
  application_raw    = text_or(tds[2])
  on_notice_to_raw   = text_or(tds[3])
  link_el            = tds[4]&.at_css("a")
  document_url 	     = link_el ? abs_url(URL, link_el["href"]) : ""

  date_received      = Util.parse_aus_date(application_raw)
  on_notice_to       = Util.parse_aus_date(on_notice_to_raw)

  # Reference: try text, then file name, then a stable fallback
  council_reference  = extract_reference(description) ||
                       extract_reference(address) ||
                       extract_reference(File.basename(document_url)) ||
                       begin
                         date_key = date_received || on_notice_to
                         base = [date_key&.strftime("%Y%m%d"), address.gsub(/\s+/, " ")[0,40]].compact.join(" ")
                         base.empty? ? "GSBC-ROW-#{idx+1}" : "GSBC #{base}"
                       end

  next if address.empty? || council_reference.empty?

  upsert_and_enrich!(
    table: TABLE,
    row: {
      description: description,
      date_received: date_received,
      date_received_raw: application_raw,
      address: address,
      council_reference: council_reference,
      applicant: "",
      owner: ""
    },
    extras: { document_url: document_url }
  )
  saved += 1
end

puts "Done #{TABLE}. Saved #{saved} item(s)."
