import { z } from "zod/v4";
import { ProcessStatus, RuntimeStatus, ChatScopeType, ConfigEtag, ContentHash, DpWorkerRuntimeMode, DpWorkerUpgradeStatus, IanaTimezone, LlmProtocol, ProxyProtocol, SocialPlatform, TaskRunStatus, TaskSource, UserRole, Uuid, } from "./domain.js";
/**
 * Database row schemas — mirror the Postgres tables described in
 * docs/architecture.md §Data model. These are the canonical TypeScript shapes
 * that DAL code (Workers `src/lib/db.ts`) returns; do not hand-roll row types
 * elsewhere.
 *
 * All timestamps are ISO-8601 strings here (postgres.js returns Date but we
 * normalize at the DAL boundary). Nullable columns are modelled with .optional().
 */
const Timestamps = {
    created_at: z.string().datetime(),
    updated_at: z.string().datetime(),
};
export const TenantRow = z.object({
    id: Uuid,
    name: z.string().min(1),
    ...Timestamps,
});
export const UserRow = z.object({
    id: Uuid,
    email: z.string().email(),
    role: UserRole,
    ...Timestamps,
});
export const ProxyRow = z.object({
    id: Uuid,
    label: z.string().min(1),
    protocol: ProxyProtocol,
    host: z.string().min(1),
    port: z.number().int().positive(),
    username: z.string().min(1),
    password: z.string().min(1),
    /** IANA timezone of the proxy egress. Required since v2.1 of the plan. */
    timezone: IanaTimezone,
    note: z.string().optional(),
    ...Timestamps,
});
export const LlmProfileRow = z.object({
    id: Uuid,
    name: z.string().min(1),
    protocol: LlmProtocol,
    base_url: z.string().min(1),
    api_key: z.string().min(1),
    model: z.string().min(1),
    image_model: z.string().optional(),
    image_api_key: z.string().optional(),
    image_size: z.string().optional(),
    ...Timestamps,
});
export const DirectorRow = z.object({
    id: Uuid,
    name: z.string().min(1),
    api_key: z.string().min(1),
    host_label: z.string().min(1),
    max_concurrent_tasks: z.number().int().nonnegative().default(0),
    last_heartbeat_at: z.string().datetime().optional(),
    version: z.string().optional(),
    dp_worker_version: z.string().optional(),
    dp_worker_latest_version: z.string().optional(),
    dp_worker_runtime_mode: DpWorkerRuntimeMode.optional(),
    dp_worker_upgrade_status: DpWorkerUpgradeStatus.optional(),
    dp_worker_upgrade_message: z.string().optional(),
    dp_worker_upgrade_started_at: z.string().datetime().optional(),
    dp_worker_upgrade_finished_at: z.string().datetime().optional(),
    ...Timestamps,
});
export const WorkerRow = z.object({
    id: Uuid,
    name: z.string().min(1),
    director_id: Uuid.optional(),
    api_key: z.string().min(1),
    llm_profile_id: Uuid,
    proxy_id: Uuid.optional(),
    /**
     * Per-dp-worker Chrome headless flag (M6). Defaults to false at the schema
     * level; operators flip it via Web UI.
     */
    headless: z.boolean().default(false),
    /**
     * Browser automation backend (cs). "chrome" = real Chrome + CDP + OmniFlow;
     * "cloakbrowser" = patched stealth Chromium via the cloakbrowser package.
     * Operators pick this at worker creation; PATCHable later.
     */
    browser_automation_mode: z.enum(["chrome", "cloakbrowser"]).default("chrome"),
    identity_id: Uuid.nullable().optional(),
    process_status: ProcessStatus,
    process_observed_at: z.string().datetime().optional(),
    runtime_status: RuntimeStatus,
    runtime_observed_at: z.string().datetime().optional(),
    last_heartbeat_at: z.string().datetime().optional(),
    current_task_id: Uuid.optional(),
    config_etag: ConfigEtag,
    session_content_hash: ContentHash.optional(),
    telegram_bot_token: z.string().optional(),
    telegram_allowed_users: z.array(z.number().int()).optional(),
    ...Timestamps,
});
export const SocialAccountRow = z.object({
    id: Uuid,
    identity_id: Uuid,
    platform: SocialPlatform,
    profile_name: z.string().default(""),
    login_type: z.enum(["password", "oauth"]).default("password"),
    username: z.string().nullable(),
    email: z.string().nullable(),
    password: z.string().nullable(),
    /** NULL = inherit identity.persona_text; non-null = own persona. */
    persona_text: z.string().nullable(),
    persona_other_json: z.record(z.string(), z.unknown()).optional(),
    enabled: z.boolean().default(true),
    ...Timestamps,
});
export const WorkerGroupRow = z.object({
    id: Uuid,
    name: z.string().min(1),
    description: z.string().optional(),
    ...Timestamps,
});
export const WorkerGroupMemberRow = z.object({
    group_id: Uuid,
    worker_id: Uuid,
    added_at: z.string().datetime(),
});
export const ChromeSessionRow = z.object({
    id: Uuid,
    identity_id: Uuid,
    r2_object_key: z.string().min(1),
    content_hash: ContentHash,
    size_bytes: z.number().int().positive(),
    uploaded_by: Uuid,
    uploaded_at: z.string().datetime(),
    note: z.string().optional(),
});
export const ScheduledTaskRow = z.object({
    id: Uuid,
    worker_id: Uuid,
    name: z.string().min(1),
    cron: z.string().min(1),
    timezone: z.string().optional(),
    prompt: z.string().min(1),
    random_delay_seconds: z.number().int().nonnegative().default(0),
    enabled: z.boolean(),
    last_run_at: z.string().datetime().optional(),
    last_status: z.enum(["ok", "error"]).optional(),
    run_count: z.number().int().nonnegative().default(0),
    success_count: z.number().int().nonnegative().default(0),
    created_by: Uuid.optional(),
    ...Timestamps,
});
export const TaskRunRow = z.object({
    id: Uuid,
    worker_id: Uuid,
    scheduled_task_id: Uuid.optional(),
    source: TaskSource,
    prompt: z.string().min(1),
    target_descriptor_json: z.record(z.string(), z.unknown()).optional(),
    status: TaskRunStatus,
    dispatched_at: z.string().datetime().optional(),
    started_at: z.string().datetime().optional(),
    finished_at: z.string().datetime().optional(),
    result_summary: z.string().optional(),
    error: z.string().optional(),
    randomized_delay_ms: z.number().int().nonnegative().default(0),
    scheduled_fire_at: z.string().datetime().optional(),
    created_by: Uuid.optional(),
    parent_adhoc_task_id: Uuid.optional(),
    ...Timestamps,
});
export const AdhocTaskBatchRow = z.object({
    id: Uuid,
    name: z.string().min(1),
    prompt: z.string().min(1),
    target_descriptor_json: z.record(z.string(), z.unknown()),
    random_delay_seconds: z.number().int().nonnegative().default(0),
    dispatch_window_seconds: z.number().int().nonnegative().default(0),
    created_by: Uuid.optional(),
    created_at: z.string().datetime(),
    summary_stats_json: z.record(z.string(), z.unknown()).optional(),
});
export const TaskDispatchQueueRow = z.object({
    id: Uuid,
    worker_id: Uuid,
    task_run_id: Uuid,
    eta: z.string().datetime(),
    priority: z.number().int().default(0),
    attempts: z.number().int().nonnegative().default(0),
    ...Timestamps,
});
export const ChatRow = z.object({
    id: Uuid,
    scope_type: ChatScopeType,
    scope_id: Uuid,
    last_message_at: z.string().datetime().optional(),
    ...Timestamps,
});
export const ChatMessageRow = z.object({
    id: Uuid,
    chat_id: Uuid,
    role: z.string().min(1),
    content_json: z.unknown(),
    attachment_ids: z.array(Uuid).default([]),
    task_run_id: Uuid.optional(),
    sender_user_id: Uuid.optional(),
    created_at: z.string().datetime(),
});
export const AttachmentRow = z.object({
    id: Uuid,
    chat_id: Uuid.optional(),
    r2_object_key: z.string().min(1),
    filename: z.string().min(1),
    mime: z.string().optional(),
    size: z.number().int().positive(),
    uploaded_by: Uuid,
    created_at: z.string().datetime(),
});
export const AuditLogRow = z.object({
    id: Uuid,
    actor_user_id: Uuid.optional(),
    actor_machine_id: Uuid.optional(),
    action: z.string().min(1),
    target_type: z.string().min(1),
    target_id: z.string().min(1),
    payload_json: z.record(z.string(), z.unknown()).optional(),
    created_at: z.string().datetime(),
});
//# sourceMappingURL=db-rows.js.map