## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup-design, message = FALSE--------------------------------------------
library(tidycreel)

# Four-week season: 10 weekdays, 8 weekend days sampled
calendar <- data.frame(
  date = as.Date(c(
    # Weekdays
    "2024-07-01", "2024-07-02", "2024-07-03", "2024-07-04", "2024-07-05",
    "2024-07-09", "2024-07-10", "2024-07-11", "2024-07-12", "2024-07-16",
    # Weekends
    "2024-07-06", "2024-07-07", "2024-07-13", "2024-07-14",
    "2024-07-20", "2024-07-21", "2024-07-27", "2024-07-28"
  )),
  day_type = c(rep("weekday", 10), rep("weekend", 8))
)

design <- creel_design(calendar, date = date, strata = day_type)
design

## ----prog-start, message = FALSE----------------------------------------------
starts <- generate_progressive_start(
  open_start    = "06:00",
  open_end      = "16:00",
  circuit_time  = 2,          # τ = 2 h; T = 10 h → k = 5 valid starts
  strategy      = "discrete",
  n             = nrow(calendar),
  seed          = 42
)
starts

## ----count-data, message = FALSE----------------------------------------------
set.seed(7)
counts <- data.frame(
  date = calendar$date,
  day_type = calendar$day_type,
  n_anglers = c(
    # Weekday counts: moderate activity
    18L, 22L, 15L, 12L, 25L, 20L, 17L, 14L, 23L, 19L,
    # Weekend counts: higher activity
    48L, 55L, 42L, 61L, 53L, 47L, 58L, 64L
  ),
  shift_hours = 10
)

## ----add-counts, message = FALSE----------------------------------------------
design <- add_counts(
  design, counts,
  count_type = "progressive",
  circuit_time = 2,
  period_length_col = shift_hours
)

# Per-day expanded effort (C × T_d) stored in design$counts
head(design$counts, 4)

## ----verify-formula-----------------------------------------------------------
# Verify: C × T_d = 18 × 10 = 180
design$counts$n_anglers[1]

## ----effort, message = FALSE--------------------------------------------------
effort <- estimate_effort(design)
effort$estimates

## ----interviews, message = FALSE----------------------------------------------
set.seed(7)
n_int <- 120 # interviews collected across the season

int_dates <- sample(calendar$date, n_int, replace = TRUE)
catch_total <- rpois(n_int, lambda = 2.1)
interviews <- data.frame(
  date = int_dates,
  day_type = calendar$day_type[match(int_dates, calendar$date)],
  trip_status = "complete",
  hours_fished = round(pmax(rnorm(n_int, mean = 3.8, sd = 1.3), 0.5), 1),
  catch_total = catch_total,
  catch_kept = pmin(rpois(n_int, lambda = 0.7), catch_total)
)

design <- add_interviews(
  design, interviews,
  trip_status = trip_status,
  catch = catch_total,
  effort = hours_fished,
  n_anglers = 1, # every interview is a single angler
  harvest = catch_kept
)

## ----catch, message = FALSE---------------------------------------------------
cpue <- estimate_catch_rate(design)
cpue$estimates

total_catch <- estimate_total_catch(design)
total_catch$estimates

## ----harvest, message = FALSE-------------------------------------------------
harvest_rate <- estimate_harvest_rate(design)
harvest_rate$estimates

total_harvest <- estimate_total_harvest(design)
total_harvest$estimates

## ----pope, message = FALSE----------------------------------------------------
cal_pope <- data.frame(
  date     = as.Date(c("2024-06-01", "2024-06-02")),
  day_type = c("weekday", "weekday")
)
des_pope <- creel_design(cal_pope, date = date, strata = day_type)

cnt_pope <- data.frame(
  date        = as.Date(c("2024-06-01", "2024-06-02")),
  day_type    = c("weekday", "weekday"),
  n_anglers   = c(234L, 200L),
  shift_hours = c(8, 8)
)
des_pope <- add_counts(
  des_pope, cnt_pope,
  count_type = "progressive",
  circuit_time = 2,
  period_length_col = shift_hours
)

# First-day Ê_d = 234 × 8 = 1,872 angler-hours
des_pope$counts

## ----multi-period, message = FALSE--------------------------------------------
# Two circuits per day (morning and evening traversals)
cal_2p <- data.frame(
  date     = rep(as.Date(c("2024-07-01", "2024-07-02", "2024-07-06", "2024-07-07")), 1),
  day_type = c("weekday", "weekday", "weekend", "weekend")
)
des_2p <- creel_design(cal_2p, date = date, strata = day_type)

cnt_2p <- data.frame(
  date = rep(as.Date(c(
    "2024-07-01", "2024-07-02",
    "2024-07-06", "2024-07-07"
  )), each = 2),
  day_type = rep(c("weekday", "weekday", "weekend", "weekend"), each = 2),
  count_time = rep(c("am", "pm"), 4),
  n_anglers = c(22L, 18L, 20L, 24L, 55L, 48L, 62L, 58L)
)

# Note: count_time_col for within-day identification
des_2p <- add_counts(des_2p, cnt_2p, count_time_col = count_time)

est_2p <- estimate_effort(des_2p)
est_2p$estimates

## ----summary, message = FALSE-------------------------------------------------
summary_tbl <- season_summary(list(
  effort  = effort,
  catch   = total_catch,
  harvest = total_harvest
))

summary_tbl$table

