// Task data store: CRUD helpers, sorting, and seed data

#include "esp_timer.h"

#include "api/tasks/store.hpp"
#include "api/users/store.hpp"

// Find a task by ID, returns nullptr if not found
task_t *find_task(uint16 id)
{
  for (int i = 0; i < MAX_TASKS; i++)
  {
    if (g_Tasks[i].active && g_Tasks[i].id == id)
    {
      return &g_Tasks[i];
    }
  }
  return nullptr;
}

// Add a task, returns pointer to new task or nullptr if full
task_t *add_task(uint8 user_id, const char *title, int64 due_date, uint8 period,
                 uint8 recurrence)
{
  // Verify user exists
  if (find_user(user_id) == nullptr)
  {
    return nullptr;
  }

  for (int i = 0; i < MAX_TASKS; i++)
  {
    if (!g_Tasks[i].active)
    {
      g_Tasks[i].id = g_NextTaskId++;
      g_Tasks[i].user_id = user_id;
      strlcpy(g_Tasks[i].title, title, sizeof(g_Tasks[i].title));
      g_Tasks[i].due_date = due_date;
      g_Tasks[i].period = period;
      g_Tasks[i].recurrence = recurrence;
      g_Tasks[i].completed = false;
      g_Tasks[i].active = true;
      return &g_Tasks[i];
    }
  }
  return nullptr;
}

// Remove a task by ID, returns true if found and removed
bool remove_task(uint16 id)
{
  for (int i = 0; i < MAX_TASKS; i++)
  {
    if (g_Tasks[i].active && g_Tasks[i].id == id)
    {
      g_Tasks[i].active = false;
      g_Tasks[i].id = 0;
      return true;
    }
  }
  return false;
}

// Remove all tasks belonging to a user
void remove_tasks_for_user(uint8 user_id)
{
  for (int i = 0; i < MAX_TASKS; i++)
  {
    if (g_Tasks[i].active && g_Tasks[i].user_id == user_id)
    {
      g_Tasks[i].active = false;
      g_Tasks[i].id = 0;
    }
  }
}

// Simple insertion sort for small arrays — sort task pointers by due_date
// ascending
void sort_tasks_by_due_date(task_t **arr, int count)
{
  for (int i = 1; i < count; i++)
  {
    task_t *key = arr[i];
    int j = i - 1;
    while (j >= 0 && arr[j]->due_date > key->due_date)
    {
      arr[j + 1] = arr[j];
      j--;
    }
    arr[j + 1] = key;
  }
}

// Populate dummy tasks on boot for development iteration.
// Uses relative offsets from current time so due dates always make sense.
void seed_tasks()
{
  int64 now = (int64)(esp_timer_get_time() / 1000000);

  // Alice's tasks (user_id = 1) — mix of one-off and recurring
  add_task(1, "Buy groceries", now + 86400, PERIOD_MORNING);
  add_task(1, "Review PR #42", now + 3600, PERIOD_AFTERNOON);
  add_task(1, "Book dentist appointment", now + 172800, PERIOD_MORNING);
  add_task(1, "Update resume", now + 604800, PERIOD_EVENING);

  // Bob's tasks (user_id = 2) — some recurring routines
  add_task(2, "Morning standup", 0, PERIOD_MORNING, 0x1F); // Mon-Fri
  add_task(2, "Deploy staging", now + 43200, PERIOD_AFTERNOON);
  add_task(2, "Write unit tests", now + 259200, PERIOD_MORNING);

  // Charlie's tasks (user_id = 3) — kid routine examples
  add_task(3, "Breakfast", 0, PERIOD_MORNING, 0x1F);  // Mon-Fri
  add_task(3, "Homework", 0, PERIOD_AFTERNOON, 0x15); // Mon+Wed+Fri
  add_task(3, "Bath time", 0, PERIOD_EVENING, 0x7F);  // Every day
}
