/**
 * Class data model for Prism RPG
 *
 * Classes in Prism provide:
 * - Archetype classification (Faith, Mana, Wanderer, Warrior)
 * - Class features gained at different levels
 * - Proficiencies and abilities
 * - Characters can have up to 3 classes simultaneously
 */
import { SYSTEM } from "../config/system.mjs"

export default class PrismRPGClass extends foundry.abstract.TypeDataModel {
  static defineSchema() {
    const fields = foundry.data.fields
    const schema = {}

    schema.description = new fields.HTMLField({
      required: true,
      textSearch: true,
      initial: ""
    })

    // Archetype (Faith, Mana, Wanderer, Warrior)
    schema.archetype = new fields.StringField({
      required: true,
      initial: "wanderer",
      choices: SYSTEM.CLASS_ARCHETYPE,
      label: "Archetype"
    })

    // Is this a Core Class or Continental Class?
    schema.classType = new fields.StringField({
      required: true,
      initial: "core",
      choices: SYSTEM.CLASS_TYPE,
      label: "Class Type"
    })

    // For Continental Classes, specify the continent
    schema.continent = new fields.StringField({
      required: false,
      initial: "",
      label: "Continent"
    })

    // Current level in this class (for tracking progression)
    schema.level = new fields.NumberField({
      required: true,
      nullable: false,
      integer: true,
      initial: 1,
      min: 1,
      max: 10,
      label: "Class Level"
    })

    // Class Features (structured data for features gained at each level)
    schema.features = new fields.SchemaField({
      level1: new fields.HTMLField({ initial: "" }),
      level2: new fields.HTMLField({ initial: "" }),
      level3: new fields.HTMLField({ initial: "" }),
      level4: new fields.HTMLField({ initial: "" }),
      level5: new fields.HTMLField({ initial: "" }),
      level6: new fields.HTMLField({ initial: "" }),
      level7: new fields.HTMLField({ initial: "" }),
      level8: new fields.HTMLField({ initial: "" }),
      level9: new fields.HTMLField({ initial: "" }),
      level10: new fields.HTMLField({ initial: "" })
    })

    // Advancements (list of advancements per level with icon, name and description)
    const advancementSchema = () => new fields.ArrayField(
      new fields.SchemaField({
        icon: new fields.StringField({
          required: true,
          initial: "",
          label: "Icon"
        }),
        name: new fields.StringField({
          required: true,
          initial: "",
          label: "Name"
        }),
        description: new fields.HTMLField({
          required: true,
          initial: "",
          label: "Description"
        })
      }),
      { initial: [] }
    )

    schema.advancements = new fields.SchemaField({
      level1: advancementSchema(),
      level2: advancementSchema(),
      level3: advancementSchema(),
      level4: advancementSchema(),
      level5: advancementSchema(),
      level6: advancementSchema(),
      level7: advancementSchema(),
      level8: advancementSchema(),
      level9: advancementSchema(),
      level10: advancementSchema()
    })

    // Proficiencies granted by this class
    schema.weaponProficiencies = new fields.StringField({
      required: true,
      initial: "",
      label: "Weapon Proficiencies"
    })

    schema.armorProficiencies = new fields.StringField({
      required: true,
      initial: "",
      label: "Armor Proficiencies"
    })

    // Special abilities
    schema.spellcasting = new fields.BooleanField({
      required: true,
      initial: false,
      label: "Has Spellcasting"
    })

    schema.spellcastingType = new fields.StringField({
      required: false,
      nullable: true,
      initial: null,
      blank: true,
      choices: SYSTEM.CLASS_SPELLCASTING_TYPE,
      label: "Spellcasting Type"
    })

    // Attribute bonuses
    schema.attributeBonuses = new fields.HTMLField({
      required: true,
      initial: "",
      label: "Attribute Bonuses"
    })

    // Additional notes
    schema.notes = new fields.HTMLField({
      required: true,
      initial: "",
      label: "Notes"
    })

    return schema
  }

  /** @override */
  static LOCALIZATION_PREFIXES = ["PRISMRPG.Class"]

  /**
   * Clean up data before validation
   * @override
   */
  static cleanData(source = {}, options = {}) {
    // Convert empty string to null for spellcastingType
    if (source.spellcastingType === "") {
      source.spellcastingType = null
    }
    return super.cleanData(source, options)
  }

  /**
   * Get the localized archetype label
   */
  get archetypeLabel() {
    return game.i18n.localize(`PRISMRPG.Class.Archetype.${this.archetype}`)
  }

  /**
   * Get the current level's features
   */
  get currentLevelFeatures() {
    return this.features[`level${this.level}`] || ""
  }

  /**
   * Get all features up to current level
   */
  get allFeaturesUpToLevel() {
    const features = []
    for (let i = 1; i <= this.level; i++) {
      const feature = this.features[`level${i}`]
      if (feature) {
        features.push({ level: i, description: feature })
      }
    }
    return features
  }

  /**
   * Get the current level's advancements
   */
  get currentLevelAdvancements() {
    return this.advancements[`level${this.level}`] || []
  }

  /**
   * Get all advancements up to current level
   */
  get allAdvancementsUpToLevel() {
    const advancements = []
    for (let i = 1; i <= this.level; i++) {
      const levelAdvancements = this.advancements[`level${i}`]
      if (levelAdvancements && levelAdvancements.length > 0) {
        advancements.push({
          level: i,
          advancements: levelAdvancements
        })
      }
    }
    return advancements
  }

  /**
   * Get advancements for a specific level
   */
  getAdvancementsForLevel(level) {
    if (level < 1 || level > 10) return []
    return this.advancements[`level${level}`] || []
  }
}
