import React, { useState } from 'react';
import { Form, Input, Button, Card, message } from 'antd';
import { MailOutlined } from '@ant-design/icons';
import { useNavigate, Link } from 'react-router-dom';
import api from '../../utils/api';
import { commonStyles } from '../../theme';

function ForgotPassword() {
  const navigate = useNavigate();
  const [loading, setLoading] = useState(false);

  const onFinish = async (values) => {
    setLoading(true);
    try {
      await api.post('/auth/forgot-password', values);
      message.success('Password reset link sent to your email');
      navigate('/login');
    } catch (error) {
      message.error('Failed to send reset email: ' + (error.response?.data?.detail || 'Unknown error'));
    } finally {
      setLoading(false);
    }
  };

  return (
    <Card title="Forgot Password" style={{ maxWidth: 400, margin: '0 auto', ...commonStyles.cardGray }}>
      <p>Enter your email address and we'll send you a link to reset your password.</p>
      <Form
        name="forgot-password"
        onFinish={onFinish}
        autoComplete="off"
        layout="vertical"
      >
        <Form.Item
          name="email"
          rules={[
            { required: true, message: 'Please input your email!' },
            { type: 'email', message: 'Please enter a valid email!' }
          ]}
        >
          <Input prefix={<MailOutlined />} placeholder="Email" />
        </Form.Item>

        <Form.Item>
          <Button type="primary" htmlType="submit" loading={loading} block>
            Send Reset Link
          </Button>
        </Form.Item>

        <Form.Item>
          <Link to="/login">Back to Login</Link>
        </Form.Item>
      </Form>
    </Card>
  );
}

export default ForgotPassword;