// Auto-generated Daily-logs service
import { queryAll, queryOne, execute } from "../db/client.js";
import type { Daily-logs, CreateDaily-logsInput, UpdateDaily-logsInput } from "../types/index.js";

export class Daily-logsService {
  /** List all daily-logs */
  list(): Daily-logs[] {
    return queryAll<Daily-logs>("SELECT * FROM daily-logs ORDER BY created_at DESC");
  }

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

  /** Create */
  create(input: CreateDaily-logsInput): Daily-logs {
    const id = crypto.randomUUID();
    const now = new Date().toISOString();
    const cols = ["id", "user_id", "title", "data", "created_at", "updated_at"];
    const values = [id, input.userId ?? null, input.title ?? null, input.data ?? null, now, now];

    execute(`INSERT INTO daily-logs (${cols.join(", ")}) VALUES (?, ?, ?, ?, ?, ?)`, values);
    return this.getById(id)!;
  }

  /** Update */
  update(id: string, input: UpdateDaily-logsInput): Daily-logs | undefined {
    const existing = this.getById(id);
    if (!existing) return undefined;

    const sets: string[] = [];
    const values: unknown[] = [];
    if (input.userId !== undefined) { sets.push("user_id = ?"); values.push(input.userId); }
    if (input.title !== undefined) { sets.push("title = ?"); values.push(input.title); }
    if (input.data !== undefined) { sets.push("data = ?"); values.push(input.data); }

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

    sets.push("updated_at = ?");
    values.push(new Date().toISOString());

    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;
  }
}
