import {FormEvent, useState} from "react"
import "../style/form.css"
// @ts-ignore
import {useNavigate, useParams} from "react-router-dom"
import {useAppFetcher} from "../App.tsx";
import {Failure} from "../app/Failure.ts";
export default function AddMemberPage() {

    const [errors, setErrors] = useState<Failure[]>([])
    const fetcher = useAppFetcher()
    const navigate = useNavigate()
    const { teamId } = useParams()

    async function handleSubmit(e : FormEvent){
        e.preventDefault()

        const { email,role} = Object.fromEntries(
            new FormData(e.target as HTMLFormElement),
        )
        console.log(email,role)
        const response = await fetcher.fetchAPI(
            "teams/"+teamId+"/members",
            {email,role},
            "POST"
        )

        if(response.ok){
            const teamId = await response.json()
            navigate("/team/"+teamId)
            return
        }

        try {
            const failures = await response.json()
            setErrors(
                Object.entries<string[]>(failures).map(([type, messages]) => ({
                    type,
                    messages,
                })),
            )
        } catch (e) {
            setErrors([
                {
                    type: "internal error",
                    messages: ["an internal error occurred."],
                },
            ])
        }

    }

    return (
        <div className="container">
            <h2>Ajouter un membre à votre équipe</h2>

            {errors.map(({ type, messages }) =>
                messages.map((message) => (
                    <p key={type} style={{ color: "red" }}>
                        {type} : {message}
                    </p>
                )),
            )}

            <form onSubmit={handleSubmit}>
                <div className="form-group">
                    <label htmlFor="email">Email du membre :</label>
                    <input type="text" id="email" name="email" required/>

                        <fieldset className="role">
                            <legend>Rôle du membre dans l'équipe :</legend>
                            <div className="radio">
                                <label htmlFor="P">Joueur</label>
                                <input type="radio" id="P" name="role" value="PLAYER" checked />
                            </div>
                            <div className="radio">
                                <label htmlFor="C">Coach</label>
                                <input type="radio" id="C" name="role" value="COACH" />
                            </div>
                        </fieldset>

                        <div id="buttons">
                            <input
                                className="button"
                                type="submit"
                                value="Confirmer"
                            />
                        </div>
                </div>
            </form>

        </div>


    )

}