import { DateTime } from 'luxon' import { BaseModel, column, belongsTo } from '@adonisjs/lucid/orm' import type { BelongsTo } from '@adonisjs/lucid/types/relations' import Product from '#models/product' export interface SupplierPricePoint { price: number date?: string source?: string [k: string]: any } export interface EbayData { sold: Array> sale: Array> } /** * Snapshot dữ liệu mỗi lần lấy về để đưa AI gợi ý giá. * Lưu nguyên đầu vào (supplier + eBay) phục vụ audit & re-run. */ export default class History extends BaseModel { @column({ isPrimary: true }) declare id: number @column() declare username: string @column() declare productId: number /** [{ price, date, source, ... }, ...] — lịch sử giá nguồn (ERP, supplier). */ @column({ columnName: 'data_sources', prepare: (v) => JSON.stringify(v ?? []), consume: (v) => (typeof v === 'string' ? JSON.parse(v) : v), }) declare dataSources: SupplierPricePoint[] /** { sold: [...], sale: [...] } — dữ liệu eBay (đã bán / đang bán). */ @column({ columnName: 'data_ebay', prepare: (v) => JSON.stringify(v ?? { sold: [], sale: [] }), consume: (v) => (typeof v === 'string' ? JSON.parse(v) : v), }) declare dataEbay: EbayData /** Kết quả AI tại thời điểm đó (tùy chọn). */ @column({ columnName: 'ai_result', prepare: (v) => (v === null || v === undefined ? null : JSON.stringify(v)), consume: (v) => (typeof v === 'string' ? JSON.parse(v) : v), }) declare aiResult: Record | null @column.dateTime({ autoCreate: true, columnName: 'time' }) declare time: DateTime @belongsTo(() => Product) declare product: BelongsTo }