/**
 * Modèles Sequelize — portage direct de apps/content/models.py et
 * apps/chatbot/models.py (Django/PostgreSQL) vers MySQL.
 *
 * Traductions : django-modeltranslation générait des colonnes
 * `<champ>_fr` / `<champ>_en` automatiquement. On les déclare ici
 * explicitement (même convention de nommage) ; voir src/utils/translate.js
 * pour la résolution du champ à servir selon la langue active.
 */
const { DataTypes } = require("sequelize");
const sequelize = require("../config/database");

// --- Category ------------------------------------------------------------
const Category = sequelize.define("Category", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  name_fr: { type: DataTypes.STRING(100), allowNull: false },
  name_en: { type: DataTypes.STRING(100), allowNull: true },
  slug: { type: DataTypes.STRING(110), allowNull: false, unique: true },
}, { tableName: "categories" });

// --- Page ------------------------------------------------------------------
const Page = sequelize.define("Page", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  title_fr: { type: DataTypes.STRING(200), allowNull: false },
  title_en: { type: DataTypes.STRING(200), allowNull: true },
  slug: { type: DataTypes.STRING(220), allowNull: false, unique: true },
  body_fr: { type: DataTypes.TEXT, allowNull: false },
  body_en: { type: DataTypes.TEXT, allowNull: true },
  is_published: { type: DataTypes.BOOLEAN, defaultValue: true },
  order: { type: DataTypes.INTEGER, defaultValue: 0 },
}, { tableName: "pages" });

// --- Article ---------------------------------------------------------------
const Article = sequelize.define("Article", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  title_fr: { type: DataTypes.STRING(250), allowNull: false },
  title_en: { type: DataTypes.STRING(250), allowNull: true },
  slug: { type: DataTypes.STRING(270), allowNull: false, unique: true },
  excerpt_fr: { type: DataTypes.STRING(400), defaultValue: "" },
  excerpt_en: { type: DataTypes.STRING(400), allowNull: true },
  body_fr: { type: DataTypes.TEXT, allowNull: false },
  body_en: { type: DataTypes.TEXT, allowNull: true },
  cover_image: { type: DataTypes.STRING(500), allowNull: true },
  category_id: { type: DataTypes.INTEGER, allowNull: true },
  is_published: { type: DataTypes.BOOLEAN, defaultValue: true },
  is_featured: { type: DataTypes.BOOLEAN, defaultValue: false },
  published_at: { type: DataTypes.DATE, allowNull: true },
}, { tableName: "articles" });

// --- Event -------------------------------------------------------------
const Event = sequelize.define("Event", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  title_fr: { type: DataTypes.STRING(250), allowNull: false },
  title_en: { type: DataTypes.STRING(250), allowNull: true },
  slug: { type: DataTypes.STRING(270), allowNull: false, unique: true },
  description_fr: { type: DataTypes.TEXT, allowNull: false },
  description_en: { type: DataTypes.TEXT, allowNull: true },
  location_fr: { type: DataTypes.STRING(250), defaultValue: "" },
  location_en: { type: DataTypes.STRING(250), allowNull: true },
  theme_fr: { type: DataTypes.STRING(250), defaultValue: "" },
  theme_en: { type: DataTypes.STRING(250), allowNull: true },
  start_date: { type: DataTypes.DATE, allowNull: false },
  end_date: { type: DataTypes.DATE, allowNull: true },
  registration_url: { type: DataTypes.STRING(500), defaultValue: "" },
  programme_url: { type: DataTypes.STRING(500), defaultValue: "" },
  cover_image: { type: DataTypes.STRING(500), allowNull: true },
  is_published: { type: DataTypes.BOOLEAN, defaultValue: true },
  manually_closed: { type: DataTypes.BOOLEAN, defaultValue: false },
}, {
  tableName: "events",
  getterMethods: {
    // équivalent de la @property status du modèle Django
    status() {
      const now = new Date();
      if (this.manually_closed) return "past";
      const end = this.end_date || this.start_date;
      if (now < new Date(this.start_date)) return "upcoming";
      if (now <= new Date(end)) return "ongoing";
      return "past";
    },
  },
});

// --- Document --------------------------------------------------------------
const Document = sequelize.define("Document", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  title: { type: DataTypes.STRING(250), allowNull: false },
  description: { type: DataTypes.TEXT, defaultValue: "" },
  file: { type: DataTypes.STRING(500), allowNull: false },
  is_public: { type: DataTypes.BOOLEAN, defaultValue: true },
  extracted_text: { type: DataTypes.TEXT, defaultValue: "" },
}, { tableName: "documents" });

// --- ContactMessage ----------------------------------------------------
const ContactMessage = sequelize.define("ContactMessage", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  name: { type: DataTypes.STRING(150), allowNull: false },
  email: { type: DataTypes.STRING(254), allowNull: false },
  subject: { type: DataTypes.STRING(250), defaultValue: "" },
  message: { type: DataTypes.TEXT, allowNull: false },
  language: { type: DataTypes.STRING(2), defaultValue: "fr" },
  is_read: { type: DataTypes.BOOLEAN, defaultValue: false },
}, { tableName: "contact_messages" });

// --- Stat --------------------------------------------------------------
const Stat = sequelize.define("Stat", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  value: { type: DataTypes.STRING(20), allowNull: false },
  label_fr: { type: DataTypes.STRING(150), allowNull: false },
  label_en: { type: DataTypes.STRING(150), allowNull: true },
  order: { type: DataTypes.INTEGER, defaultValue: 0 },
}, { tableName: "stats" });

// --- Partner -------------------------------------------------------------
const Partner = sequelize.define("Partner", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  name: { type: DataTypes.STRING(200), allowNull: false },
  logo: { type: DataTypes.STRING(500), allowNull: false },
  website_url: { type: DataTypes.STRING(500), defaultValue: "" },
  partner_type: {
    type: DataTypes.ENUM("sponsor", "institutional"),
    defaultValue: "sponsor",
  },
  order: { type: DataTypes.INTEGER, defaultValue: 0 },
}, { tableName: "partners" });

// --- Person ----------------------------------------------------------------
const Person = sequelize.define("Person", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  full_name: { type: DataTypes.STRING(200), allowNull: false },
  title_fr: { type: DataTypes.STRING(250), defaultValue: "" },
  title_en: { type: DataTypes.STRING(250), allowNull: true },
  bio_fr: { type: DataTypes.TEXT, defaultValue: "" },
  bio_en: { type: DataTypes.TEXT, allowNull: true },
  photo: { type: DataTypes.STRING(500), allowNull: true },
  role_type: {
    type: DataTypes.ENUM("committee", "speaker"),
    defaultValue: "committee",
  },
  event_id: { type: DataTypes.INTEGER, allowNull: true },
  linkedin_url: { type: DataTypes.STRING(500), defaultValue: "" },
  order: { type: DataTypes.INTEGER, defaultValue: 0 },
}, { tableName: "people" });

// --- ImportantLink -------------------------------------------------------
const ImportantLink = sequelize.define("ImportantLink", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  label_fr: { type: DataTypes.STRING(150), allowNull: false },
  label_en: { type: DataTypes.STRING(150), allowNull: true },
  url: { type: DataTypes.STRING(500), allowNull: false },
  order: { type: DataTypes.INTEGER, defaultValue: 0 },
}, { tableName: "important_links" });

// --- GalleryImage ------------------------------------------------------
const GalleryImage = sequelize.define("GalleryImage", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  image: { type: DataTypes.STRING(500), allowNull: false },
  caption_fr: { type: DataTypes.STRING(250), defaultValue: "" },
  caption_en: { type: DataTypes.STRING(250), allowNull: true },
  event_id: { type: DataTypes.INTEGER, allowNull: true },
  order: { type: DataTypes.INTEGER, defaultValue: 0 },
}, { tableName: "gallery_images" });

// --- HeroSlide -----------------------------------------------------------
const HeroSlide = sequelize.define("HeroSlide", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  image: { type: DataTypes.STRING(500), allowNull: false },
  caption: { type: DataTypes.STRING(250), defaultValue: "" },
  order: { type: DataTypes.INTEGER, defaultValue: 0 },
  is_published: { type: DataTypes.BOOLEAN, defaultValue: true },
}, { tableName: "hero_slides" });

// --- Testimonial -----------------------------------------------------------
const Testimonial = sequelize.define("Testimonial", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  quote_fr: { type: DataTypes.TEXT, allowNull: false },
  quote_en: { type: DataTypes.TEXT, allowNull: true },
  author_name: { type: DataTypes.STRING(150), allowNull: false },
  author_role_fr: { type: DataTypes.STRING(200), defaultValue: "" },
  author_role_en: { type: DataTypes.STRING(200), allowNull: true },
  photo: { type: DataTypes.STRING(500), allowNull: true },
  order: { type: DataTypes.INTEGER, defaultValue: 0 },
  is_published: { type: DataTypes.BOOLEAN, defaultValue: true },
}, { tableName: "testimonials" });

// --- FAQ ---------------------------------------------------------------
const FAQ = sequelize.define("FAQ", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  question_fr: { type: DataTypes.STRING(300), allowNull: false },
  question_en: { type: DataTypes.STRING(300), allowNull: true },
  answer_fr: { type: DataTypes.TEXT, allowNull: false },
  answer_en: { type: DataTypes.TEXT, allowNull: true },
  order: { type: DataTypes.INTEGER, defaultValue: 0 },
}, { tableName: "faqs" });

// === apps/chatbot/models.py =================================================

const KnowledgeItem = sequelize.define("KnowledgeItem", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  question_fr: { type: DataTypes.STRING(300), allowNull: false },
  question_en: { type: DataTypes.STRING(300), allowNull: true },
  answer_fr: { type: DataTypes.TEXT, allowNull: false },
  answer_en: { type: DataTypes.TEXT, allowNull: true },
  is_published: { type: DataTypes.BOOLEAN, defaultValue: true },
  order: { type: DataTypes.INTEGER, defaultValue: 0 },
}, { tableName: "knowledge_items" });

const KnowledgeChunk = sequelize.define("KnowledgeChunk", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  source_type: {
    type: DataTypes.ENUM("knowledge_item", "faq", "document", "article", "page"),
    allowNull: false,
  },
  source_id: { type: DataTypes.INTEGER, allowNull: false },
  language: { type: DataTypes.STRING(2), defaultValue: "fr" },
  content: { type: DataTypes.TEXT, allowNull: false },
  // MySQL n'a pas d'équivalent standard à pgvector : le vecteur (768
  // dimensions) est stocké en JSON et la similarité cosinus est calculée
  // en mémoire côté Node (voir services/chatbotService.js). Largement
  // suffisant pour un volume de quelques centaines de fragments.
  embedding: { type: DataTypes.JSON, allowNull: true },
}, {
  tableName: "knowledge_chunks",
  updatedAt: false,
  indexes: [{ fields: ["source_type", "source_id"] }],
});

const ChatSession = sequelize.define("ChatSession", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  session_id: { type: DataTypes.STRING(64), allowNull: false, unique: true },
  language: { type: DataTypes.STRING(2), defaultValue: "fr" },
}, { tableName: "chat_sessions", updatedAt: false });

const ChatMessage = sequelize.define("ChatMessage", {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  session_id: { type: DataTypes.INTEGER, allowNull: false },
  role: { type: DataTypes.ENUM("user", "assistant"), allowNull: false },
  content: { type: DataTypes.TEXT, allowNull: false },
  answer_source: { type: DataTypes.STRING(10), defaultValue: "" },
}, { tableName: "chat_messages", updatedAt: false });

// --- Associations ------------------------------------------------------
Article.belongsTo(Category, { foreignKey: "category_id", as: "category" });
Category.hasMany(Article, { foreignKey: "category_id", as: "articles" });

Person.belongsTo(Event, { foreignKey: "event_id", as: "event" });
Event.hasMany(Person, { foreignKey: "event_id", as: "speakers" });

GalleryImage.belongsTo(Event, { foreignKey: "event_id", as: "event" });
Event.hasMany(GalleryImage, { foreignKey: "event_id", as: "gallery_images" });

ChatMessage.belongsTo(ChatSession, { foreignKey: "session_id", as: "session" });
ChatSession.hasMany(ChatMessage, { foreignKey: "session_id", as: "messages" });

module.exports = {
  sequelize,
  Category, Page, Article, Event, Document, ContactMessage,
  Stat, Partner, Person, ImportantLink, GalleryImage, HeroSlide, Testimonial, FAQ,
  KnowledgeItem, KnowledgeChunk, ChatSession, ChatMessage,
};
