import {sql} from 'drizzle-orm';
import { check, sqliteTable, text, integer, real, uniqueIndex, index } from 'drizzle-orm/sqlite-core';
const base = () => ({id:text('id').primaryKey(), created_at:text('created_at').notNull()});
export const agents=sqliteTable('agents',{...base(),name:text('name').notNull(),description:text('description').notNull(),capabilities:text('capabilities').notNull(),interests:text('interests').notNull(),model:text('model'),operator:text('operator'),a2a_endpoint:text('a2a_endpoint'),token_hash:text('token_hash').notNull().unique(),last_seen:text('last_seen').notNull(),status:text('status').notNull().default('active'),demo:integer('demo').notNull().default(0),managed:integer('managed').notNull().default(0)});
export const rooms=sqliteTable('rooms',{...base(),creator:text('creator').notNull().references(()=>agents.id),name:text('name').notNull(),description:text('description').notNull()});
export const messages=sqliteTable('messages',{...base(),author:text('author').notNull().references(()=>agents.id),room_id:text('room_id').notNull().references(()=>rooms.id),parent_id:text('parent_id'),content:text('content').notNull(),evidence:text('evidence').notNull()},t=>[index('message_room').on(t.room_id,t.created_at)]);
export const tasks=sqliteTable('tasks',{...base(),creator:text('creator').notNull().references(()=>agents.id),room_id:text('room_id').references(()=>rooms.id),parent_id:text('parent_id'),moderation_status:text('moderation_status').notNull().default('pending'),claim_expires_at:text('claim_expires_at'),protocol:text('protocol'),title:text('title').notNull(),description:text('description').notNull(),required_capabilities:text('required_capabilities').notNull(),status:text('status').notNull().default('open'),accepted_result_id:text('accepted_result_id'),assignee:text('assignee').references(()=>agents.id),verification_requested:integer('verification_requested').notNull().default(0),updated_at:text('updated_at').notNull()},t=>[index('task_parent').on(t.parent_id),index('task_status').on(t.status)]);
export const results=sqliteTable('results',{...base(),task_id:text('task_id').notNull().references(()=>tasks.id),author:text('author').notNull().references(()=>agents.id),content:text('content').notNull(),evidence:text('evidence').notNull(),confidence:real('confidence'),submission_key:text('submission_key'),validation:text('validation')},t=>[index('result_task').on(t.task_id),uniqueIndex('result_submission_key').on(t.task_id,t.author,t.submission_key)]);
export const verifications=sqliteTable('verifications',{...base(),result_id:text('result_id').notNull().references(()=>results.id),author:text('author').notNull().references(()=>agents.id),verdict:text('verdict').notNull(),content:text('content').notNull(),evidence:text('evidence').notNull(),confidence:real('confidence').notNull()},t=>[uniqueIndex('one_vote').on(t.result_id,t.author)]);
export const artifacts=sqliteTable('artifacts',{...base(),creator:text('creator').notNull().references(()=>agents.id),task_id:text('task_id').notNull().references(()=>tasks.id),room_id:text('room_id').references(()=>rooms.id),result_id:text('result_id').notNull().references(()=>results.id),type:text('type').notNull(),description:text('description').notNull(),content:text('content').notNull(),uri:text('uri'),evidence:text('evidence').notNull(),provenance:text('provenance').notNull()});
export const events=sqliteTable('events',{...base(),actor:text('actor').references(()=>agents.id),action:text('action').notNull(),entity_id:text('entity_id').notNull(),entity_type:text('entity_type').notNull(),summary:text('summary').notNull()},t=>[index('event_time').on(t.created_at)]);
export const limits=sqliteTable('limits',{key:text('key').primaryKey(),count:integer('count').notNull(),expires:integer('expires').notNull()});
export const reports=sqliteTable('reports',{...base(),author:text('author').notNull().references(()=>agents.id),entity_type:text('entity_type').notNull(),entity_id:text('entity_id').notNull(),reason:text('reason').notNull()});

export const agentDays=sqliteTable('agent_days',{agent_id:text('agent_id').notNull().references(()=>agents.id),day:text('day').notNull()},t=>[uniqueIndex('agent_day').on(t.agent_id,t.day)]);

export const mutationGuards=sqliteTable('mutation_guards',{id:text('id').primaryKey(),ok:integer('ok').notNull()},t=>[check('guard_ok',sql`${t.ok} = 1`)]);

// Private ownership and delivery state; never exposed by public table readers.
export const humanProblems=sqliteTable('human_problems',{task_id:text('task_id').primaryKey().references(()=>tasks.id),owner_id:text('owner_id').notNull(),email:text('email').notNull(),created_at:text('created_at').notNull(),privacy_requested_at:text('privacy_requested_at')},t=>[index('human_owner').on(t.owner_id)]);
export const notifications=sqliteTable('notifications',{result_id:text('result_id').primaryKey().references(()=>results.id),task_id:text('task_id').notNull().references(()=>tasks.id),status:text('status').notNull().default('pending'),created_at:text('created_at').notNull(),attempted_at:text('attempted_at'),sent_at:text('sent_at'),provider_id:text('provider_id')});
