import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:http_parser/http_parser.dart';
import 'package:monitoring/config.dart';

class EditProfileScreen extends StatefulWidget {
  final String token;

  const EditProfileScreen({super.key, required this.token});

  @override
  State<EditProfileScreen> createState() => _EditProfileScreenState();
}

class _EditProfileScreenState extends State<EditProfileScreen> {
  final _formKey = GlobalKey<FormState>();
  bool _loading = true;
  bool _saving = false;

  final _nameController = TextEditingController();
  final _alamatController = TextEditingController();
  final _tempatLahirController = TextEditingController();
  final _tanggalLahirController = TextEditingController();

  String? _jenisKelamin;
  String? _fotoUrl;
  File? _selectedImage;

  @override
  void initState() {
    super.initState();
    fetchProfile();
  }

  Future<void> fetchProfile() async {
    try {
      final dio = Dio(
        BaseOptions(
          headers: {
            'Authorization': 'Bearer ${widget.token}',
            'Accept': 'application/json',
          },
        ),
      );

      final response = await dio.get('$baseUrl/santri/me');
      final data = response.data;

      final user = data['user'];
      final santri = data['santri'];

      setState(() {
        _nameController.text = user['name'] ?? '';
        _alamatController.text = santri['alamat'] ?? '';
        _tempatLahirController.text = santri['tempat_lahir'] ?? '';
        _tanggalLahirController.text = santri['tanggal_lahir'] ?? '';
        _jenisKelamin = santri['jenis_kelamin'];
        _fotoUrl = santri['foto_url'];
        _loading = false;
      });
    } catch (e) {
      debugPrint('Gagal ambil profil: $e');
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('Gagal mengambil profil')));
      Navigator.pop(context);
    }
  }

  Future<void> pickImage() async {
    final picked = await ImagePicker().pickImage(source: ImageSource.gallery);
    if (picked != null) {
      setState(() {
        _selectedImage = File(picked.path);
      });
    }
  }

  Future<void> saveProfile() async {
    if (!_formKey.currentState!.validate()) return;

    setState(() => _saving = true);

    try {
      final dio = Dio(
        BaseOptions(
          headers: {
            'Authorization': 'Bearer ${widget.token}',
            'Accept': 'application/json',
            'Content-Type': 'multipart/form-data',
          },
        ),
      );

      final formData = FormData.fromMap({
        'name': _nameController.text.trim(),
        'alamat': _alamatController.text.trim(),
        'tempat_lahir': _tempatLahirController.text.trim(),
        'tanggal_lahir': _tanggalLahirController.text.trim(),
        'jenis_kelamin': _jenisKelamin,
        if (_selectedImage != null)
          'foto': await MultipartFile.fromFile(
            _selectedImage!.path,
            filename: _selectedImage!.path.split('/').last,
            contentType: MediaType('image', 'jpeg'),
          ),
      });

      final response = await dio.post('$baseUrl/santri/me', data: formData);

      final message = response.data['message'] ?? 'Profil berhasil diperbarui';

      if (context.mounted) {
        ScaffoldMessenger.of(
          context,
        ).showSnackBar(SnackBar(content: Text(message)));
        Navigator.pop(context, true); // Kembali & beri sinyal refresh
      }
    } on DioException catch (e) {
      final msg =
          e.response?.data['message'] ??
          e.response?.data.toString() ??
          'Gagal memperbarui profil';
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
    } finally {
      setState(() => _saving = false);
    }
  }

  @override
  void dispose() {
    _nameController.dispose();
    _alamatController.dispose();
    _tempatLahirController.dispose();
    _tanggalLahirController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    const fieldSpace = SizedBox(height: 16);

    return Scaffold(
      appBar: AppBar(title: const Text('Edit Profil')),
      body:
          _loading
              ? const Center(child: CircularProgressIndicator())
              : SingleChildScrollView(
                padding: const EdgeInsets.all(16),
                child: Form(
                  key: _formKey,
                  child: Column(
                    children: [
                      if (_fotoUrl != null && _selectedImage == null)
                        CircleAvatar(
                          backgroundImage: NetworkImage(_fotoUrl!),
                          radius: 45,
                        ),
                      if (_selectedImage != null)
                        CircleAvatar(
                          backgroundImage: FileImage(_selectedImage!),
                          radius: 45,
                        ),
                      TextButton.icon(
                        onPressed: pickImage,
                        icon: const Icon(Icons.photo),
                        label: const Text('Ubah Foto'),
                      ),
                      fieldSpace,

                      TextFormField(
                        controller: _nameController,
                        decoration: const InputDecoration(
                          labelText: 'Nama Lengkap',
                          border: OutlineInputBorder(),
                        ),
                        validator: (val) => val!.isEmpty ? 'Wajib diisi' : null,
                      ),
                      fieldSpace,

                      TextFormField(
                        controller: _alamatController,
                        decoration: const InputDecoration(
                          labelText: 'Alamat',
                          border: OutlineInputBorder(),
                        ),
                      ),
                      fieldSpace,

                      TextFormField(
                        controller: _tempatLahirController,
                        decoration: const InputDecoration(
                          labelText: 'Tempat Lahir',
                          border: OutlineInputBorder(),
                        ),
                      ),
                      fieldSpace,

                      TextFormField(
                        controller: _tanggalLahirController,
                        decoration: const InputDecoration(
                          labelText: 'Tanggal Lahir',
                          hintText: 'YYYY-MM-DD',
                          border: OutlineInputBorder(),
                        ),
                      ),
                      fieldSpace,

                      DropdownButtonFormField<String>(
                        value: _jenisKelamin,
                        decoration: const InputDecoration(
                          labelText: 'Jenis Kelamin',
                          border: OutlineInputBorder(),
                        ),
                        items: const [
                          DropdownMenuItem(
                            value: 'L',
                            child: Text('Laki-laki'),
                          ),
                          DropdownMenuItem(
                            value: 'P',
                            child: Text('Perempuan'),
                          ),
                        ],
                        onChanged: (val) => setState(() => _jenisKelamin = val),
                      ),

                      const SizedBox(height: 28),
                      SizedBox(
                        width: double.infinity,
                        child: ElevatedButton.icon(
                          onPressed: _saving ? null : saveProfile,
                          icon: const Icon(Icons.save),
                          label:
                              _saving
                                  ? const SizedBox(
                                    width: 20,
                                    height: 20,
                                    child: CircularProgressIndicator(
                                      strokeWidth: 2,
                                    ),
                                  )
                                  : const Text('Simpan Perubahan'),
                          style: ElevatedButton.styleFrom(
                            padding: const EdgeInsets.symmetric(vertical: 16),
                            backgroundColor: Colors.green,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(12),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
    );
  }
}
