import { randomUUID } from "node:crypto";
import { getPool } from "../db/pool";
function nowIso() {
    return new Date().toISOString();
}
function makeCode(id) {
    return `d_${id.slice(0, 6)}`;
}
function mapDevice(row) {
    return {
        id: row.id,
        device_code: row.device_code,
        serial_number: row.serial_number || undefined,
        vendor: row.vendor || undefined,
        model: row.model || undefined,
        communication_mode: row.communication_mode,
        capability_profile_json: row.capability_profile_json || {},
        auth_method: row.auth_method || undefined,
        status: row.status,
        last_seen_at: row.last_seen_at || undefined,
        firmware_version: row.firmware_version || undefined,
        created_at: row.created_at,
        updated_at: row.updated_at
    };
}
export async function createDevice(payload) {
    const id = randomUUID();
    const now = nowIso();
    const { rows } = await getPool().query(`INSERT INTO devices (
      id,
      device_code,
      serial_number,
      vendor,
      model,
      communication_mode,
      capability_profile_json,
      auth_method,
      status,
      last_seen_at,
      firmware_version,
      created_at,
      updated_at
    ) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)
    RETURNING *`, [
        id,
        payload.device_code || makeCode(id),
        payload.serial_number,
        payload.vendor,
        payload.model,
        payload.communication_mode || "static",
        payload.capability_profile_json || {},
        payload.auth_method || "token",
        payload.status || "active",
        payload.last_seen_at || null,
        payload.firmware_version,
        now,
        now
    ]);
    return mapDevice(rows[0]);
}
export async function listDevices() {
    const { rows } = await getPool().query("SELECT * FROM devices ORDER BY created_at DESC");
    return rows.map(mapDevice);
}
export async function getDeviceById(id) {
    const { rows } = await getPool().query("SELECT * FROM devices WHERE id = $1", [id]);
    return rows[0] ? mapDevice(rows[0]) : null;
}
export async function patchDevice(id, patch) {
    const existing = await getDeviceById(id);
    if (!existing) {
        throw new Error("DEVICE_NOT_FOUND");
    }
    const merged = { ...existing, ...patch, updated_at: nowIso() };
    const { rows } = await getPool().query(`UPDATE devices
      SET device_code = $2,
          serial_number = $3,
          vendor = $4,
          model = $5,
          communication_mode = $6,
          capability_profile_json = $7,
          auth_method = $8,
          status = $9,
          firmware_version = $10,
          last_seen_at = $11,
          updated_at = $12
      WHERE id = $1
      RETURNING *`, [
        id,
        merged.device_code,
        merged.serial_number,
        merged.vendor,
        merged.model,
        merged.communication_mode || "static",
        merged.capability_profile_json || {},
        merged.auth_method,
        merged.status,
        merged.firmware_version,
        merged.last_seen_at || null,
        merged.updated_at
    ]);
    return mapDevice(rows[0]);
}
export function toDevicePayload(device) {
    return { ...device };
}
