/**
 * Shared field factory functions for actor DataModels.
 * All field names match template.json exactly.
 * TODO: migrate to SR6AttributeField / SR6SkillField / SR6ConditionMonitorField
 *       per the sprite actor pattern once field naming is modernised.
 */

/** Returns a standard attribute SchemaField (bod/agi/rea/str/wil/log/int/cha pattern). */
export function standardAttributeField(base = 2, pool = 2) {
    const fields = foundry.data.fields;
    return new fields.SchemaField({
        base:      new fields.NumberField({ initial: base }),
        mod:       new fields.NumberField({ initial: 0 }),
        modString: new fields.StringField({ initial: "" }),
        augment:   new fields.NumberField({ initial: 0 }),
        pool:      new fields.NumberField({ initial: pool })
    });
}

/** Returns a skill SchemaField with expandedSpecializations. Most skills use this. */
export function skillField() {
    const fields = foundry.data.fields;
    return new fields.SchemaField({
        points:                  new fields.NumberField({ initial: 0 }),
        specialization:          new fields.StringField({ initial: "" }),
        expertise:               new fields.StringField({ initial: "" }),
        modifier:                new fields.NumberField({ initial: 0 }),
        augment:                 new fields.NumberField({ initial: 0 }),
        expandedSpecializations: new fields.ArrayField(new fields.StringField())
    });
}

/** Returns a skill SchemaField WITHOUT expandedSpecializations. Used for electronics only. */
export function skillFieldNoExpanded() {
    const fields = foundry.data.fields;
    return new fields.SchemaField({
        points:         new fields.NumberField({ initial: 0 }),
        specialization: new fields.StringField({ initial: "" }),
        expertise:      new fields.StringField({ initial: "" }),
        modifier:       new fields.NumberField({ initial: 0 }),
        augment:        new fields.NumberField({ initial: 0 })
    });
}

/**
 * Returns the full attributes SchemaField matching template.json attribute-template.
 * Includes bod/agi/rea/str/wil/log/int/cha, mag, res, edg, essence.
 */
export function attributesSchema() {
    const fields = foundry.data.fields;
    return new fields.SchemaField({
        // TODO: migrate each to SR6AttributeField
        bod: standardAttributeField(2, 2),
        agi: standardAttributeField(2, 2),
        rea: standardAttributeField(2, 2),
        str: standardAttributeField(2, 2),
        wil: standardAttributeField(2, 2),
        log: standardAttributeField(2, 2),
        int: standardAttributeField(2, 2),
        cha: standardAttributeField(2, 2),
        mag: new fields.SchemaField({
            base:       new fields.NumberField({ initial: 0 }),
            mod:        new fields.NumberField({ initial: 0 }),
            pool:       new fields.NumberField({ initial: 0 }),
            min:        new fields.NumberField({ initial: 0 }),
            initiation: new fields.NumberField({ initial: 0 })
        }),
        res: new fields.SchemaField({
            base:       new fields.NumberField({ initial: 0 }),
            mod:        new fields.NumberField({ initial: 0 }),
            pool:       new fields.NumberField({ initial: 0 }),
            submersion: new fields.NumberField({ initial: 0 })
        }),
        edg: new fields.SchemaField({
            current: new fields.NumberField({ initial: 0 }),
            max:     new fields.NumberField({ initial: 1 })
        }),
        essence: new fields.SchemaField({
            base: new fields.NumberField({ initial: 6 }),
            mod:  new fields.NumberField({ initial: 0 }),
            pool: new fields.NumberField({ initial: 6 })
        })
    });
}

/**
 * Returns the monitor fields (NOT wrapped in a parent SchemaField — spread directly into the
 * DataModel schema). Matches template.json monitor-template: physical, stun, overflow, edge.
 */
export function monitorsSchema() {
    const fields = foundry.data.fields;
    const monitor = (base = 9, value = 9) => new fields.SchemaField({
        base:      new fields.NumberField({ initial: base }),
        mod:       new fields.NumberField({ initial: 0 }),
        modString: new fields.StringField({ initial: "" }),
        value:     new fields.NumberField({ initial: value }),
        dmg:       new fields.NumberField({ initial: 0 }),
        max:       new fields.NumberField({ initial: 18 })
    });
    return {
        // TODO: migrate to SR6ConditionMonitorField
        physical: monitor(9, 9),
        stun:     monitor(9, 9),
        overflow: new fields.SchemaField({
            mod:       new fields.NumberField({ initial: 0 }),
            modString: new fields.StringField({ initial: "" }),
            value:     new fields.NumberField({ initial: 0 }),
            dmg:       new fields.NumberField({ initial: 0 }),
            max:       new fields.NumberField({ initial: 32 })
        }),
        edge: new fields.SchemaField({
            value: new fields.NumberField({ initial: 0 }),
            max:   new fields.NumberField({ initial: 1 })
        })
    };
}

/** Returns the initiative SchemaField (template.json initiative-template). */
export function initiativeSchema() {
    const fields = foundry.data.fields;
    const initiative = (dice = 1) => new fields.SchemaField({
        mod:     new fields.NumberField({ initial: 0 }),
        dice:    new fields.NumberField({ initial: dice }),
        diceMod: new fields.NumberField({ initial: 0 })
    });
    return new fields.SchemaField({
        // TODO: migrate to SR6InitiativeField
        astral:   initiative(2),
        matrix:   initiative(1),
        physical: initiative(1),
        actions:  new fields.NumberField({ initial: 0 })
    });
}

/** Returns the skills SchemaField (template.json skill-template, 19 skills). */
export function skillsSchema() {
    return new (foundry.data.fields.SchemaField)({
        // TODO: migrate to SR6SkillField
        astral:         skillField(),
        athletics:      skillField(),
        biotech:        skillField(),
        close_combat:   skillField(),
        con:            skillField(),
        conjuring:      skillField(),
        cracking:       skillField(),
        electronics:    skillFieldNoExpanded(), // template.json omits expandedSpecializations here
        enchanting:     skillField(),
        engineering:    skillField(),
        exotic_weapons: skillField(),
        firearms:       skillField(),
        influence:      skillField(),
        outdoors:       skillField(),
        perception:     skillField(),
        piloting:       skillField(),
        sorcery:        skillField(),
        stealth:        skillField(),
        tasking:        skillField()
    });
}
