import 'package:flutter/material.dart';
import '../../../services/restaurant_form_validator.dart';

/// 식당 추가 폼 위젯
class AddRestaurantForm extends StatelessWidget {
  final GlobalKey<FormState> formKey;
  final TextEditingController nameController;
  final TextEditingController categoryController;
  final TextEditingController subCategoryController;
  final TextEditingController descriptionController;
  final TextEditingController phoneController;
  final TextEditingController roadAddressController;
  final TextEditingController jibunAddressController;
  final TextEditingController latitudeController;
  final TextEditingController longitudeController;
  final Function(String) onFieldChanged;

  const AddRestaurantForm({
    super.key,
    required this.formKey,
    required this.nameController,
    required this.categoryController,
    required this.subCategoryController,
    required this.descriptionController,
    required this.phoneController,
    required this.roadAddressController,
    required this.jibunAddressController,
    required this.latitudeController,
    required this.longitudeController,
    required this.onFieldChanged,
  });

  @override
  Widget build(BuildContext context) {
    return Form(
      key: formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          // 가게 이름
          TextFormField(
            controller: nameController,
            decoration: InputDecoration(
              labelText: '가게 이름 *',
              hintText: '예: 맛있는 한식당',
              prefixIcon: const Icon(Icons.store),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
              ),
            ),
            onChanged: onFieldChanged,
            validator: (value) {
              if (value == null || value.isEmpty) {
                return '가게 이름을 입력해주세요';
              }
              return null;
            },
          ),
          const SizedBox(height: 16),

          // 카테고리
          Row(
            children: [
              Expanded(
                child: TextFormField(
                  controller: categoryController,
                  decoration: InputDecoration(
                    labelText: '카테고리 *',
                    hintText: '예: 한식',
                    prefixIcon: const Icon(Icons.category),
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  onChanged: onFieldChanged,
                  validator: (value) =>
                      RestaurantFormValidator.validateCategory(value),
                ),
              ),
              const SizedBox(width: 8),
              Expanded(
                child: TextFormField(
                  controller: subCategoryController,
                  decoration: InputDecoration(
                    labelText: '세부 카테고리',
                    hintText: '예: 갈비',
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  onChanged: onFieldChanged,
                ),
              ),
            ],
          ),
          const SizedBox(height: 16),

          // 설명
          TextFormField(
            controller: descriptionController,
            maxLines: 2,
            decoration: InputDecoration(
              labelText: '설명',
              hintText: '맛집에 대한 간단한 설명',
              prefixIcon: const Icon(Icons.description),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
              ),
            ),
            onChanged: onFieldChanged,
          ),
          const SizedBox(height: 16),

          // 전화번호
          TextFormField(
            controller: phoneController,
            keyboardType: TextInputType.phone,
            decoration: InputDecoration(
              labelText: '전화번호',
              hintText: '예: 02-1234-5678',
              prefixIcon: const Icon(Icons.phone),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
              ),
            ),
            onChanged: onFieldChanged,
            validator: (value) =>
                RestaurantFormValidator.validatePhoneNumber(value),
          ),
          const SizedBox(height: 16),

          // 도로명 주소
          TextFormField(
            controller: roadAddressController,
            decoration: InputDecoration(
              labelText: '도로명 주소 *',
              hintText: '예: 서울시 중구 세종대로 110',
              prefixIcon: const Icon(Icons.location_on),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
              ),
            ),
            onChanged: onFieldChanged,
            validator: (value) =>
                RestaurantFormValidator.validateAddress(value),
          ),
          const SizedBox(height: 16),

          // 지번 주소
          TextFormField(
            controller: jibunAddressController,
            decoration: InputDecoration(
              labelText: '지번 주소',
              hintText: '예: 서울시 중구 태평로1가 31',
              prefixIcon: const Icon(Icons.map),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
              ),
            ),
            onChanged: onFieldChanged,
          ),
          const SizedBox(height: 16),

          // 위도/경도 입력
          Row(
            children: [
              Expanded(
                child: TextFormField(
                  controller: latitudeController,
                  keyboardType: const TextInputType.numberWithOptions(
                    decimal: true,
                  ),
                  decoration: InputDecoration(
                    labelText: '위도',
                    hintText: '37.5665',
                    prefixIcon: const Icon(Icons.explore),
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  onChanged: onFieldChanged,
                  validator: (value) {
                    if (value != null && value.isNotEmpty) {
                      final latitude = double.tryParse(value);
                      if (latitude == null || latitude < -90 || latitude > 90) {
                        return '올바른 위도값을 입력해주세요';
                      }
                    }
                    return null;
                  },
                ),
              ),
              const SizedBox(width: 8),
              Expanded(
                child: TextFormField(
                  controller: longitudeController,
                  keyboardType: const TextInputType.numberWithOptions(
                    decimal: true,
                  ),
                  decoration: InputDecoration(
                    labelText: '경도',
                    hintText: '126.9780',
                    prefixIcon: const Icon(Icons.explore),
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  onChanged: onFieldChanged,
                  validator: (value) {
                    if (value != null && value.isNotEmpty) {
                      final longitude = double.tryParse(value);
                      if (longitude == null ||
                          longitude < -180 ||
                          longitude > 180) {
                        return '올바른 경도값을 입력해주세요';
                      }
                    }
                    return null;
                  },
                ),
              ),
            ],
          ),
          const SizedBox(height: 8),
          Text(
            '주소가 정확하지 않을 경우 위도/경도를 현재 위치로 입력합니다.',
            style: Theme.of(
              context,
            ).textTheme.bodySmall?.copyWith(color: Colors.grey),
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }
}
