#==========================================================
# Summary DataAudit Report
#==========================================================

#' Summarize a DataAudit Report
#'
#' Produces a compact summary of a data audit generated by
#' \code{\link{audit_data}}.
#'
#' @param object An object of class \code{DataAuditReport}.
#' @param ... Additional arguments, currently unused.
#'
#' @return An object of class \code{summary.DataAuditReport}
#'   containing dataset-level, variable-level, and optional
#'   custom rule summaries.
#'
#' @export
summary.DataAuditReport <- function(
    object,
    ...
) {

  #---------------------------------------------
  # Validate object
  #---------------------------------------------
  required_components <- c(
    "overview",
    "variables",
    "duplicates",
    "summary",
    "rules"
  )

  if (
    !inherits(object, "DataAuditReport") ||
    !all(required_components %in% names(object))
  ) {

    stop(
      "Invalid 'DataAuditReport' object.",
      call. = FALSE
    )
  }

  #---------------------------------------------
  # Validate dataset summary
  #---------------------------------------------
  if (!is.data.frame(object$summary)) {

    stop(
      "Invalid summary in 'DataAuditReport'.",
      call. = FALSE
    )
  }

  required_summary <- c(
    "Rows",
    "Variables",
    "VariablesWithIssues",
    "DuplicateRows",
    "TotalIssues",
    "RuleViolations",
    "OverallIssues"
  )

  if (
    !all(
      required_summary %in%
      names(object$summary)
    )
  ) {

    stop(
      "Invalid summary in 'DataAuditReport'.",
      call. = FALSE
    )
  }

  #---------------------------------------------
  # Validate variable summary
  #---------------------------------------------
  if (
    !is.data.frame(object$variables) ||
    !"Issue" %in% names(object$variables)
  ) {

    stop(
      "Invalid variable summary in 'DataAuditReport'.",
      call. = FALSE
    )
  }

  #---------------------------------------------
  # Validate duplicate summary
  #---------------------------------------------
  if (
    !is.data.frame(object$duplicates) ||
    !"Duplicate" %in% names(object$duplicates)
  ) {

    stop(
      "Invalid duplicate summary in 'DataAuditReport'.",
      call. = FALSE
    )
  }

  #---------------------------------------------
  # Variables with issues
  #---------------------------------------------
  variables_with_issues <-
    object$variables[
      object$variables$Issue,
      ,
      drop = FALSE
    ]

  #---------------------------------------------
  # Duplicated rows
  #---------------------------------------------
  duplicate_rows <-
    object$duplicates[
      object$duplicates$Duplicate,
      ,
      drop = FALSE
    ]

  #---------------------------------------------
  # Custom rule summary
  #---------------------------------------------
  rule_summary <- NULL

  if (!is.null(object$rules)) {

    if (
      !inherits(
        object$rules,
        "DataAuditRuleResult"
      )
    ) {

      stop(
        "Invalid rule results in 'DataAuditReport'.",
        call. = FALSE
      )
    }

    if (!is.data.frame(object$rules$summary)) {

      stop(
        "Invalid rule summary in 'DataAuditReport'.",
        call. = FALSE
      )
    }

    required_rule_summary <- c(
      "Rule",
      "Violations"
    )

    if (
      !all(
        required_rule_summary %in%
        names(object$rules$summary)
      )
    ) {

      stop(
        "Invalid rule summary in 'DataAuditReport'.",
        call. = FALSE
      )
    }

    rule_summary <-
      object$rules$summary
  }

  #---------------------------------------------
  # Construct summary
  #---------------------------------------------
  result <- list(
    dataset = object$summary,
    variables_with_issues =
      variables_with_issues,
    duplicate_rows =
      duplicate_rows,
    rules = rule_summary
  )

  class(result) <- c(
    "summary.DataAuditReport",
    "list"
  )

  result
}
