// Auto-generated DailyLog service
import { queryAll, queryOne, execute } from "../db/client.js";
import type { DailyLog, CreateDailyLogInput, UpdateDailyLogInput } from "../types/index.js";

export class DailyLogService {
  /** List all daily_logs */
  list(): DailyLog[] {
    return queryAll<DailyLog>("SELECT * FROM daily_logs ORDER BY created_at DESC");
  }

  /** Get by ID */
  getById(id: string): DailyLog | undefined {
    return queryOne<DailyLog>("SELECT * FROM daily_logs WHERE id = ?", [id]);
  }

  /** Create */
  create(input: CreateDailyLogInput): DailyLog {
    const id = crypto.randomUUID();
    const now = new Date().toISOString();
    const hasCreatedAt = false;
    const hasUpdatedAt = false;
    const cols = ["id", "pet_id", "category", "value", "logged_at"];
    const placeholders = cols.map(() => "?").join(", ");
    const values = [id, input.petId ?? null, input.category ?? null, input.value ?? null, input.loggedAt ?? null];

    execute(`INSERT INTO daily_logs (${cols.join(", ")}) VALUES (${placeholders})`, values);
    return this.getById(id)!;
  }

  /** Update */
  update(id: string, input: UpdateDailyLogInput): DailyLog | undefined {
    const existing = this.getById(id);
    if (!existing) return undefined;

    const sets: string[] = [];
    const values: unknown[] = [];
    if (input.petId !== undefined) { sets.push("pet_id = ?"); values.push(input.petId); }
    if (input.category !== undefined) { sets.push("category = ?"); values.push(input.category); }
    if (input.value !== undefined) { sets.push("value = ?"); values.push(input.value); }
    if (input.loggedAt !== undefined) { sets.push("logged_at = ?"); values.push(input.loggedAt); }

    if (sets.length === 0) return existing;

    

    values.push(id);
    execute(`UPDATE daily_logs SET ${sets.join(", ")} WHERE id = ?`, values);
    return this.getById(id)!;
  }

  /** Delete */
  delete(id: string): boolean {
    const result = execute("DELETE FROM daily_logs WHERE id = ?", [id]);
    return result.changes > 0;
  }
}
