/**
 * rTime Intent Schemas — Anoma-style resource-backed commitments.
 *
 * DocId formats:
 *   {space}:rtime:intents        → IntentsDoc (active intents pool)
 *   {space}:rtime:solver-results → SolverResultsDoc (solver recommendations)
 *   {space}:rtime:skill-curves   → SkillCurvesDoc (per-skill pricing)
 *   {space}:rtime:reputation     → ReputationDoc (per-member per-skill reputation)
 */

import type { DocSchema } from '../../shared/local-first/document';
import type { Skill } from './schemas';

// ── Intent ──

export type IntentType = 'offer' | 'need';
export type IntentStatus = 'active' | 'matched' | 'settled' | 'withdrawn';

export interface Intent {
	id: string;
	memberId: string;           // DID of the member
	memberName: string;
	type: IntentType;
	skill: Skill;
	hours: number;
	description: string;
	// Validity predicates
	minReputation?: number;     // minimum counterparty reputation (0-100)
	maxPrice?: number;          // maximum acceptable skill price (tokens/hour)
	preferredMembers?: string[];// preferred collaborator DIDs
	escrowTxId?: string;        // token escrow reference (for needs)
	status: IntentStatus;
	createdAt: number;
	expiresAt?: number;
}

// ── Solver Result ──

export type SolverResultStatus = 'proposed' | 'accepted' | 'rejected' | 'settled';

export interface SolverResult {
	id: string;
	intents: string[];          // intent IDs in this cluster
	members: string[];          // participating member IDs
	skills: Skill[];
	totalHours: number;
	score: number;              // solver confidence score (0-1)
	status: SolverResultStatus;
	acceptances: Record<string, boolean>; // memberId → accepted?
	createdAt: number;
}

// ── Skill Curve ──

export interface SkillCurveEntry {
	skill: Skill;
	supplyHours: number;        // total offered hours
	demandHours: number;        // total needed hours
	currentPrice: number;       // calculated price per hour (tokens)
	history: Array<{ price: number; timestamp: number }>;
}

// ── Reputation ──

export interface ReputationRating {
	from: string;               // rater memberId
	score: number;              // 1-5 stars
	timestamp: number;
}

export interface ReputationEntry {
	memberId: string;
	skill: Skill;
	score: number;              // 0-100 computed score
	completedHours: number;
	ratings: ReputationRating[];
}

// ── Documents ──

export interface IntentsDoc {
	meta: {
		module: string;
		collection: string;
		version: number;
		spaceSlug: string;
		createdAt: number;
	};
	intents: Record<string, Intent>;
}

export interface SolverResultsDoc {
	meta: {
		module: string;
		collection: string;
		version: number;
		spaceSlug: string;
		createdAt: number;
	};
	results: Record<string, SolverResult>;
}

export interface SkillCurvesDoc {
	meta: {
		module: string;
		collection: string;
		version: number;
		spaceSlug: string;
		createdAt: number;
	};
	curves: Record<string, SkillCurveEntry>;
}

export interface ReputationDoc {
	meta: {
		module: string;
		collection: string;
		version: number;
		spaceSlug: string;
		createdAt: number;
	};
	entries: Record<string, ReputationEntry>;
}

// ── DocId helpers ──

export function intentsDocId(space: string) {
	return `${space}:rtime:intents` as const;
}

export function solverResultsDocId(space: string) {
	return `${space}:rtime:solver-results` as const;
}

export function skillCurvesDocId(space: string) {
	return `${space}:rtime:skill-curves` as const;
}

export function reputationDocId(space: string) {
	return `${space}:rtime:reputation` as const;
}

// ── Schema registrations ──

export const intentsSchema: DocSchema<IntentsDoc> = {
	module: 'rtime',
	collection: 'intents',
	version: 1,
	init: (): IntentsDoc => ({
		meta: {
			module: 'rtime',
			collection: 'intents',
			version: 1,
			spaceSlug: '',
			createdAt: Date.now(),
		},
		intents: {},
	}),
};

export const solverResultsSchema: DocSchema<SolverResultsDoc> = {
	module: 'rtime',
	collection: 'solver-results',
	version: 1,
	init: (): SolverResultsDoc => ({
		meta: {
			module: 'rtime',
			collection: 'solver-results',
			version: 1,
			spaceSlug: '',
			createdAt: Date.now(),
		},
		results: {},
	}),
};

export const skillCurvesSchema: DocSchema<SkillCurvesDoc> = {
	module: 'rtime',
	collection: 'skill-curves',
	version: 1,
	init: (): SkillCurvesDoc => ({
		meta: {
			module: 'rtime',
			collection: 'skill-curves',
			version: 1,
			spaceSlug: '',
			createdAt: Date.now(),
		},
		curves: {},
	}),
};

export const reputationSchema: DocSchema<ReputationDoc> = {
	module: 'rtime',
	collection: 'reputation',
	version: 1,
	init: (): ReputationDoc => ({
		meta: {
			module: 'rtime',
			collection: 'reputation',
			version: 1,
			spaceSlug: '',
			createdAt: Date.now(),
		},
		entries: {},
	}),
};
