/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2023 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import { z } from "zod";
import { TeamSchema } from "./team.js";
import { PlayerSchema, ResponseUserSchema } from "./data.js";

export const ResponseGroupsSchema = z.object({
    id: z.number(),
    name: z.string(),
    pointsPerWin: z.number(),
    pointsPerLoss: z.number(),
    pointsPerDraw: z.number(),
    type: z.enum(["GROUP_STAGE", "ELIMINATION_STAGE"]),
    points: z.record(z.string(), z.number()).nullable(),
});

export const EventFightSchema = z.object({
    id: z.number(),
    spielmodus: z.string(),
    map: z.string(),
    blueTeam: TeamSchema,
    redTeam: TeamSchema,
    start: z.number(),
    ergebnis: z.number(),
    spectatePort: z.number().nullable(),
    group: ResponseGroupsSchema.nullable(),
    hasFinished: z.boolean(),
});

export type EventFight = z.infer<typeof EventFightSchema>;

export const EventFightEditSchema = EventFightSchema.omit({
    id: true,
    group: true,
    hasFinished: true,
}).extend({
    group: z.number().nullable(),
});

export type EventFightEdit = z.infer<typeof EventFightEditSchema>;

export type ResponseGroups = z.infer<typeof ResponseGroupsSchema>;

export const ResponseRelationSchema = z.object({
    id: z.number(),
    fight: z.number(),
    team: z.enum(["RED", "BLUE"]),
    type: z.enum(["FIGHT", "GROUP"]),
    fromFight: EventFightSchema.optional(),
    fromGroup: ResponseGroupsSchema.optional(),
    fromPlace: z.number(),
});

export type ResponseRelation = z.infer<typeof ResponseRelationSchema>;

export const ShortEventSchema = z.object({
    id: z.number(),
    name: z.string(),
    start: z.number(),
    end: z.number(),
});

export type ShortEvent = z.infer<typeof ShortEventSchema>;

export const SWEventSchema = ShortEventSchema.extend({
    deadline: z.number(),
    maxTeamMembers: z.number(),
    schemType: z.string().nullable(),
    publicSchemsOnly: z.boolean(),
});

export type SWEvent = z.infer<typeof SWEventSchema>;

export const ExtendedEventSchema = z.object({
    event: SWEventSchema,
    teams: z.array(TeamSchema),
    groups: z.array(ResponseGroupsSchema),
    fights: z.array(EventFightSchema),
    referees: z.array(ResponseUserSchema),
    relations: z.array(ResponseRelationSchema),
});

export type ExtendedEvent = z.infer<typeof ExtendedEventSchema>;

export const ResponseTeamSchema = z.object({
    id: z.number(),
    name: z.string(),
    kuerzel: z.string(),
    color: z.string(),
});

export type ResponseTeam = z.infer<typeof ResponseTeamSchema>;

export const CreateEventGroupSchema = z.object({
    name: z.string(),
    type: z.enum(["GROUP_STAGE", "ELIMINATION_STAGE"]),
});
export type CreateEventGroup = z.infer<typeof CreateEventGroupSchema>;

export const UpdateEventGroupSchema = z.object({
    name: z.string().nullable().optional(),
    type: z.enum(["GROUP_STAGE", "ELIMINATION_STAGE"]).nullable().optional(),
    pointsPerWin: z.number().nullable().optional(),
    pointsPerLoss: z.number().nullable().optional(),
    pointsPerDraw: z.number().nullable().optional(),
});
export type UpdateEventGroup = z.infer<typeof UpdateEventGroupSchema>;

export const GroupEditSchema = ResponseGroupsSchema.omit({
    id: true,
    points: true,
});
export type GroupUpdateEdit = z.infer<typeof GroupEditSchema>;

export const CreateEventRelationSchema = z.object({
    fightId: z.number(),
    team: z.enum(["RED", "BLUE"]),
    fromType: z.enum(["FIGHT", "GROUP"]),
    fromId: z.number(),
    fromPlace: z.number(),
});
export type CreateEventRelation = z.infer<typeof CreateEventRelationSchema>;

export const UpdateFromRelationSchema = z.object({
    fromType: z.enum(["FIGHT", "GROUP"]),
    fromId: z.number(),
    fromPlace: z.number(),
});
export type UpdateFromRelation = z.infer<typeof UpdateFromRelationSchema>;

export const UpdateEventRelationSchema = z.object({
    team: z.enum(["RED", "BLUE"]).nullable().optional(),
    from: UpdateFromRelationSchema.nullable().optional(),
});
export type UpdateEventRelation = z.infer<typeof UpdateEventRelationSchema>;
