# Kingborough Council — Planning Notices (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 this from filename: da_kingborough
URL   = "https://www.kingborough.tas.gov.au/development/planning-notices/"

DB.ensure_table!(TABLE)

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

rows = doc.css("#list 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.length < 5

  council_reference    = tds[0].text.strip
  address              = tds[1].text.strip
  date_received_raw    = tds[2].text.strip
  on_notice_to_raw     = tds[3].text.strip
  description          = tds[4].text.strip

  # All links in the row (often PDFs). Join if there are multiple.
  links = row.css("td a").map { |a| abs_url(URL, a["href"]) }.reject(&:empty?)
  document_url = links.join(", ")

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

  # Need a reference and an address to store a row
  next if council_reference.empty? || address.empty?

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

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