import { PlayerInfo } from "@/features/players/types";
import { z } from "zod";
import { TournamentInfo } from "../tournaments/types";

export interface Team {
  id: string;
  name: string;
  logo: string;
  primary_color: string;
  accent_color: string;
  song_id: string;
  song_name: string;
  song_artist: string;
  song_album: string;
  song_start: number;
  song_end: number;
  song_image_url: string;
  created: string;
  updated: string;
  players: PlayerInfo[];
  tournaments: TournamentInfo[];
  private: boolean;
}

export interface TeamInfo {
  id: string;
  name: string;
  primary_color: string;
  accent_color: string;
  logo?: string;
  players: PlayerInfo[];
  private: boolean;
}

export const teamInputSchema = z
  .object({
    name: z.string().min(1, "Team name is required").max(100, "Name too long"),
    logo: z.file("Invalid logo").optional(),
    primary_color: z
      .string()
      .regex(/^#[0-9A-F]{6}$/i, "Must be valid hex color (#FF0000)")
      .optional(),
    accent_color: z
      .string()
      .regex(/^#[0-9A-F]{6}$/i, "Must be valid hex color (#FF0000)")
      .optional(),
    song_id: z.string().max(255).optional(),
    song_name: z.string().max(255).optional(),
    song_artist: z.string().max(255).optional(),
    song_album: z.string().max(255).optional(),
    song_start: z.number().int().optional(),
    song_end: z.number().int().optional(),
    song_image_url: z.url("Invalid song image URL").optional(),
    private: z.boolean().optional(),
    players: z.array(z.string()).min(1, "At least one player is required").max(10, "Maximum 10 players allowed"),
  })
  .refine(
    (data) => {
      if (data.song_start && data.song_end) {
        return data.song_end > data.song_start;
      }
      return true;
    },
    { message: "Song end time must be after start time", path: ["song_end"] }
  );

export const teamUpdateSchema = z
  .object({
    name: z.string().min(1, "Team name is required").max(100, "Name too long").optional(),
    logo: z.file("Invalid logo").optional(),
    primary_color: z
      .string()
      .regex(/^#[0-9A-F]{6}$/i, "Must be valid hex color (#FF0000)")
      .optional(),
    accent_color: z
      .string()
      .regex(/^#[0-9A-F]{6}$/i, "Must be valid hex color (#FF0000)")
      .optional(),
    song_id: z.string().max(255).optional(),
    song_name: z.string().max(255).optional(),
    song_artist: z.string().max(255).optional(),
    song_album: z.string().max(255).optional(),
    song_start: z.number().int().optional(),
    song_end: z.number().int().optional(),
    song_image_url: z.url("Invalid song image URL").optional(),
  })
  .refine(
    (data) => {
      if (data.song_start && data.song_end) {
        return data.song_end > data.song_start;
      }
      return true;
    },
    { message: "Song end time must be after start time", path: ["song_end"] }
  );

export type TeamInput = z.infer<typeof teamInputSchema>;
export type TeamUpdateInput = z.infer<typeof teamUpdateSchema>;

export interface TeamStats {
  id: string;
  team_id: string;
  team_name: string;
  matches: number;
  wins: number;
  losses: number;
  total_cups_made: number;
  total_cups_against: number;
  margin_of_victory: number;
  margin_of_loss: number;
}
