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

## ----basic-schedule, message = FALSE------------------------------------------
library(tidycreel)

sched <- generate_schedule(
  start_date    = "2024-05-01",
  end_date      = "2024-09-30",
  n_periods     = 2,
  period_labels = c("Morning", "Afternoon"),
  sampling_rate = c(weekday = 0.30, weekend = 0.50),
  seed          = 42
)

head(sched, 8)

## ----schedule-summary---------------------------------------------------------
# Counts of scheduled days by stratum
table(sched$day_type)

## ----include-all, message = FALSE---------------------------------------------
sched_full <- generate_schedule(
  start_date    = "2024-05-01",
  end_date      = "2024-09-30",
  n_periods     = 1,
  sampling_rate = c(weekday = 0.30, weekend = 0.50),
  include_all   = TRUE,
  seed          = 42
)

# Sampled proportion by stratum
table(sched_full$day_type, sched_full$sampled)

## ----high-use-schedule, message = FALSE---------------------------------------
opener_periods <- data.frame(
  start_date = as.Date("2027-07-31"),
  end_date = as.Date("2027-08-01"),
  label = "high_use",
  reason = "opener"
)

sched_high_use <- generate_schedule(
  start_date = "2027-07-30",
  end_date = "2027-08-03",
  n_periods = 1,
  sampling_rate = c(weekday = 1, weekend = 1, high_use = 1),
  include_all = TRUE,
  expand_periods = FALSE,
  seed = 42,
  special_periods = opener_periods
)

sched_high_use[, c("date", "day_type", "final_stratum", "special_period_reason", "sampled")]

## ----high-use-audit-----------------------------------------------------------
attr(sched_high_use, "special_period_audit")
attr(sched_high_use, "special_period_allocation")

## ----high-use-diagnostics, message = FALSE------------------------------------
sched_fragile <- suppressWarnings(generate_schedule(
  start_date = "2027-08-01",
  end_date = "2027-08-08",
  n_periods = 1,
  sampling_rate = c(weekday = 1, weekend = 1, high_use = 1),
  include_all = TRUE,
  expand_periods = FALSE,
  seed = 42,
  special_periods = data.frame(
    start_date = as.Date("2027-08-07"),
    end_date = as.Date("2027-08-07"),
    label = "high_use",
    reason = "opener"
  )
))

attr(sched_fragile, "special_period_diagnostics")
head(format(sched_fragile), 6)

## ----three-periods, message = FALSE-------------------------------------------
sched3 <- generate_schedule(
  start_date    = "2024-06-01",
  end_date      = "2024-08-31",
  n_periods     = 3,
  period_labels = c("Morning", "Midday", "Evening"),
  sampling_rate = c(weekday = 0.40, weekend = 0.60),
  seed          = 101
)

head(sched3, 9)

## ----seed---------------------------------------------------------------------
sched_a <- generate_schedule("2024-06-01", "2024-06-30",
  n_periods = 1,
  sampling_rate = 0.4, seed = 99
)
sched_b <- generate_schedule("2024-06-01", "2024-06-30",
  n_periods = 1,
  sampling_rate = 0.4, seed = 99
)

identical(sched_a, sched_b)

## ----io, eval = FALSE---------------------------------------------------------
# # Save for field crews
# write_schedule(sched, "count_schedule_2024.csv")
# write_schedule(sched, "count_schedule_2024.xlsx")
# 
# # Reload — column types are preserved automatically
# sched_reload <- read_schedule("count_schedule_2024.csv")
# identical(sched, sched_reload)

## ----design, message = FALSE--------------------------------------------------
design <- creel_design(sched, date = date, strata = day_type)
design

## ----design-special, message = FALSE------------------------------------------
sched_for_analysis <- sched_high_use[, c("date", "final_stratum")]
names(sched_for_analysis)[2] <- "analysis_stratum"

design_special <- creel_design(
  sched_for_analysis,
  date = date,
  strata = analysis_stratum
)
design_special

## ----bus-schedule, message = FALSE--------------------------------------------
# Site-level selection probabilities (must sum to 1.0)
site_frame <- data.frame(
  site_id = c("North Bay", "South Cove", "Dock Area", "Main Channel"),
  p_site  = c(0.35, 0.25, 0.25, 0.15)
)

# Build bus-route sampling frame using the count schedule
bus_frame <- generate_bus_schedule(
  schedule       = sched,
  sampling_frame = site_frame,
  site           = site_id,
  p_site         = p_site,
  crew           = 1
)

bus_frame

## ----sample-size, message = FALSE---------------------------------------------
# Effort sample size to achieve CV ≤ 0.15
# N_h: total days per stratum in a 184-day season
# ybar_h: pilot mean daily effort (angler-hours) per stratum
# s2_h: pilot variance of daily effort per stratum
creel_n_effort(
  cv_target = 0.15,
  N_h = c(weekday = 132, weekend = 52),
  ybar_h = c(weekday = 280, weekend = 550),
  s2_h = c(weekday = 14400, weekend = 32400)
)

## ----count-times-random, message = FALSE--------------------------------------
library(tidycreel)
ct_random <- generate_count_times(
  start_time  = "06:00",
  end_time    = "14:00",
  strategy    = "random",
  n_windows   = 4,
  window_size = 30,
  min_gap     = 10,
  seed        = 42
)
ct_random

## ----count-times-systematic, message = FALSE----------------------------------
ct_systematic <- generate_count_times(
  start_time  = "06:00",
  end_time    = "14:00",
  strategy    = "systematic",
  n_windows   = 4,
  window_size = 30,
  min_gap     = 10,
  seed        = 42
)
ct_systematic

## ----count-times-fixed, message = FALSE---------------------------------------
fw <- data.frame(
  start_time = c("07:00", "09:30", "12:00"),
  end_time = c("07:30", "10:00", "12:30"),
  stringsAsFactors = FALSE
)
ct_fixed <- generate_count_times(strategy = "fixed", fixed_windows = fw)
ct_fixed

## ----count-times-progressive, message = FALSE---------------------------------
prog_starts <- generate_progressive_start(
  open_start   = "06:00",
  open_end     = "16:00",   # T = 10 h
  circuit_time = 2,          # τ = 2 h → k = 5 discrete start offsets
  strategy     = "discrete",
  n            = 10,
  seed         = 99
)
prog_starts

## ----count-times-export, eval = FALSE-----------------------------------------
# write_schedule(ct_systematic, "count_times_2024.csv")

## ----attach-count-times, message = FALSE--------------------------------------
sched <- generate_schedule(
  start_date = "2024-06-01", end_date = "2024-06-07",
  n_periods = 2,
  sampling_rate = c(weekday = 0.5, weekend = 0.8),
  seed = 42
)
ct <- generate_count_times(
  start_time = "06:00", end_time = "14:00",
  strategy = "systematic", n_windows = 3,
  window_size = 30, min_gap = 10,
  seed = 42
)
field_schedule <- attach_count_times(sched, ct)
field_schedule

## ----validate-design, message = FALSE-----------------------------------------
report <- validate_design(
  N_h        = c(weekday = 132, weekend = 52),
  ybar_h     = c(weekday = 280, weekend = 550),
  s2_h       = c(weekday = 14400, weekend = 32400),
  n_proposed = c(weekday = 40L, weekend = 26L),
  cv_target  = 0.15
)
report

## ----validate-design-results, message = FALSE---------------------------------
report$results

## ----check-completeness, message = FALSE--------------------------------------
data(example_calendar)
data(example_counts)
data(example_interviews)

design <- creel_design(example_calendar, date = date, strata = day_type)
design <- add_counts(design, example_counts)
design <- add_interviews(design, example_interviews,
  catch        = catch_total,
  effort       = hours_fished,
  trip_status  = trip_status
)
comp <- check_completeness(design)
comp

## ----season-summary, eval = FALSE---------------------------------------------
# # Run estimators first (see the tidycreel workflow vignette)
# effort <- estimate_effort(design)
# cpue <- estimate_catch_rate(design)
# 
# # Assemble the season summary
# summary_tbl <- season_summary(list(effort = effort, cpue = cpue))
# summary_tbl$table

## ----season-summary-export, eval = FALSE--------------------------------------
# write_schedule(summary_tbl$table, "season_2024_summary.xlsx")

