test_that("fix_date_char works for a series of malformed dates", {
  dates <- c(
    "02 03 2021" = "2021-03-02",
    "15/07/11" = "2011-07-15",
    "15/07/84" = "1984-07-15",
    "2015 11 11" = "2015-11-11",
    "05/2015" = "2015-05-01",
    "2020-01" = "2020-01-01",
    "05/89" = "1989-05-01",
    "02/14" = "2014-02-01",
    "1994" = "1994-07-01"
  )

  fixed <- fix_date_char(names(dates))

  expect_equal(fixed, as.Date(unname(dates)))
})

test_that("fix_date returns NA if NA is given", {
  bad.date <- NA
  fixed.date <- fix_date_char(bad.date)
  expect_equal(fixed.date, as.Date(NA))
})

test_that("error if date given isn't character", {
  bad.date <- 15
  expect_error(fix_date_char(bad.date), "date should be a character \n")
})

test_that("error if date given has over 4 digits for year", {
  bad.date <- "202312-12-2"
  expect_error(fix_date_char(bad.date), "unable to tidy a date")
})

test_that("Test NA imputed when provided", {
  bad.date <- "1990-5"
  expect_warning(
    date.guess <- fix_date_char(bad.date, day.impute = NA),
    "NA imputed"
  )
  expect_equal(date.guess, as.Date(NA))
})

test_that("fix_date works for mdy format", {
  bad.date <- "07/15/11"
  fixed.date <- fix_date_char(bad.date, format = "mdy")
  expect_equal(fixed.date, as.Date("2011-07-15"))
})

test_that("unexpected format raises error", {
  bad.date <- "07/15/11"
  expect_error(
    fix_date_char(bad.date, format = "ydm"),
    "format should be either 'dmy' or 'mdy' \n"
  )
})

test_that("'de' and 'del' is parsed", {
  expect_equal(fix_date_char("20 de abril de 1994"), as.Date("1994-04-20"))
  expect_equal(fix_date_char("06 de enero del 2008"), as.Date("2008-01-06"))
})

test_that("German format days are supported", {
  expect_equal(fix_date_char("29.08.1992"), as.Date("1992-08-29"))
})

test_that("nunber. + month name (whitespace) + year", {
  expect_equal(fix_date_char("3. Oktober 1990"), as.Date("1990-10-03"))
})

test_that("Month-first dates where the month is given by name", {
  expect_equal(fix_date_char("July 4th, 1776"), as.Date("1776-07-04"))
})

test_that("Non-excel numeric date is parsed correctly", {
  expect_equal(fix_date_char("19374"), as.Date("2023-01-17"))
})

test_that("Excel numeric date is parsed correctly", {
  expect_equal(fix_date_char("41035", excel = TRUE), as.Date("2012-05-06"))
})

test_that("Allow single digit day with double digit year", {
  expect_equal(fix_date_char("03/10/90"), fix_date_char("3/10/90"))
})

test_that("if day > max for month, lower day to max", {
  expect_equal(fix_date_char("2023-02-30"), as.Date("2023-02-28"))
  expect_equal(fix_date_char("2024-02-30"), as.Date("2024-02-29"))
})

test_that("Error if day of month > 31 or < 1", {
  expect_error(fix_date_char("32-01-2023"), "Day not in expected range\n")
})


test_that("Roman conversion works as expected", {
  expect_equal(
    fix_date_char("2023-II-15", roman.numeral = TRUE),
    as.Date("2023-02-15")
  )
})

test_that("NA values in vectors are preserved correctly", {
  # Test vector with mixed NA, empty string, and "NA" string
  test_vector <- c("2023-01-01", NA, "2024-01-01", "", "NA")
  result <- fix_date_char(test_vector)

  # Check that valid dates are parsed correctly
  expect_equal(result[1], as.Date("2023-01-01"))
  expect_equal(result[3], as.Date("2024-01-01"))

  # Check that all types of NA are preserved as NA
  expect_true(is.na(result[2])) # NA
  expect_true(is.na(result[4])) # empty string
  expect_true(is.na(result[5])) # "NA" string

  # Ensure we didn't get the placeholder date (1999-01-01)
  expect_false(any(result == as.Date("1999-01-01"), na.rm = TRUE))
})

test_that("Vector with all NA values returns all NA", {
  test_vector <- c(NA, "", "NA")
  result <- fix_date_char(test_vector)

  # All should be NA
  expect_true(all(is.na(result)))
  expect_equal(length(result), 3)

  # Ensure we didn't get the placeholder date
  expect_false(any(result == as.Date("1999-01-01"), na.rm = TRUE))
})

test_that("? is treated as NA", {
  expect_equal(fix_date_char("?"), as.Date(NA))
  expect_equal(
    fix_date_char(c("01/01/2020", "?", "02/02/2021")),
    as.Date(c("2020-01-01", NA, "2021-02-02"))
  )
})

test_that("error if date has more than two separators", {
  expect_error(fix_date_char("01/02/03/2000"), "unable to tidy a date")
  expect_error(fix_date_char("01-02-03-2000"), "unable to tidy a date")
  expect_error(fix_date_char("01 02 03 2000"), "unable to tidy a date")
})

test_that("parse error includes element index and date", {
  expect_error(
    fix_date_char("202312-12-2"),
    "for subject 1 \\(date: 202312\\-12\\-2\\)"
  )
})

test_that("parse error reports correct index when error is not in first element", {
  expect_error(
    fix_date_char(c("01/01/2020", "02/02/2021", "202312-12-2")),
    "for subject 3 \\(date: 202312\\-12\\-2\\)"
  )
})

test_that("Legitimate 1999-01-01 dates are preserved correctly", {
  # Test vector with legitimate 1999-01-01 dates mixed with NA and other dates
  test_vector <- c("1999-01-01", NA, "2023-01-01", "", "January 1, 1999", "NA")
  result <- fix_date_char(test_vector)

  # Check that legitimate 1999-01-01 dates are preserved
  expect_equal(result[1], as.Date("1999-01-01"))
  expect_equal(result[5], as.Date("1999-01-01")) # "January 1, 1999"
  expect_equal(result[3], as.Date("2023-01-01"))

  # Check that NA values are still NA
  expect_true(is.na(result[2])) # NA
  expect_true(is.na(result[4])) # empty string
  expect_true(is.na(result[6])) # "NA" string

  # Count legitimate 1999-01-01 dates
  count_1999 <- sum(!is.na(result) & result == as.Date("1999-01-01"))
  expect_equal(count_1999, 2)
})
