import React, { useState, ChangeEvent, FormEvent } from 'react';
import { useForm, SubmitHandler } from 'react-hook-form';
import { TextField, Button, Container, Typography, Box } from '@mui/material';
import axios, { AxiosResponse } from 'axios';
import { SignUpFormData, ApiResponse } from '../../types/auth';
import axiosInstance from '../../http/axiosInstance';

const SignUp = () => {
    const { register, handleSubmit, formState: { errors } } = useForm<SignUpFormData>({
        defaultValues: {
            email: '',
            login: '',
            phone: '',
            name: '',
            surname: '',
            is_active: true,
            password: '',
        }
    })


    const onSubmit: SubmitHandler<SignUpFormData> = async (data) => {
        try {
            const response: AxiosResponse<ApiResponse> = await axiosInstance.post(`${import.meta.env.VITE_API_AUTH_URL}/auth/user`, data);
            console.log('Успешная регистрация:', response.data);
        } catch (error) {
            console.error('Ошибка регистрации:', error);
        }
    };

    return (
        <Container maxWidth="sm">
            <Box my={4}>
                <Typography variant="h4" component="h1" gutterBottom>
                    Регистрация
                </Typography>
                <form onSubmit={handleSubmit(onSubmit)}>
                    <TextField
                        fullWidth
                        margin="normal"
                        label="Email"
                        {...register('email', { required: 'Email обязателен' })}
                        error={!!errors.email}
                        helperText={errors.email?.message}
                    />
                    <TextField
                        fullWidth
                        margin="normal"
                        label="Логин"
                        {...register('login', { required: 'Логин обязателен' })}
                        error={!!errors.login}
                        helperText={errors.login?.message}
                    />
                    <TextField
                        fullWidth
                        margin="normal"
                        label="Телефон"
                        {...register('phone')}
                        error={!!errors.phone}
                        helperText={errors.phone?.message}
                    />
                    <TextField
                        fullWidth
                        margin="normal"
                        label="Имя"
                        {...register('name')}
                        error={!!errors.name}
                        helperText={errors.name?.message}
                    />
                    <TextField
                        fullWidth
                        margin="normal"
                        label="Фамилия"
                        {...register('surname')}
                        error={!!errors.surname}
                        helperText={errors.surname?.message}
                    />
                    <TextField
                        fullWidth
                        margin="normal"
                        type="password"
                        label="Пароль"
                        {...register('password', { required: 'Пароль обязателен' })}
                        error={!!errors.password}
                        helperText={errors.password?.message}
                    />
                    <Button type="submit" variant="contained" color="primary">
                        Зарегистрироваться
                    </Button>
                </form>
            </Box>
        </Container>
    );
};

export default SignUp;