import {TransactType} from "./index";

export interface BattleTypeData {
    id: string
    name: string
    emoji: string
    display_order: number
}

export interface BattleTypeRelationshipData {
    attacker: string
    defender: string
    damage_percentage: number
}

export interface BattleTypeTablesData {
    types: BattleTypeData[]
    relationships: BattleTypeRelationshipData[]
}

export interface BattleTypeTable {
    getTypesAndRelationships(): Promise<BattleTypeTablesData>
}

export class BattleTypeTableImpl implements BattleTypeTable {
    private readonly _transact: TransactType

    constructor({transact}: { transact: TransactType }) {
        this._transact = transact
    }

    async getTypesAndRelationships(): Promise<BattleTypeTablesData> {
        const result: BattleTypeTablesData = {
            types: [],
            relationships: [],
        }
        await this._transact({
            readonly: true,
            async transaction(query) {
                const types = await query<{ id: string, name: string, emoji: string, display_order: number }>(
                    `SELECT id, name, emoji, display_order
                     FROM battle_types`
                )
                const relationships = await query<{ attacker: string, defender: string, damage_percentage: number }>(
                    `SELECT attacker, defender, damage_percentage
                     FROM battle_types_relationships`
                )
                result.types = types.rows
                result.relationships = relationships.rows
            }
        })
        return result
    }
}