import 'package:autos/core/theme/app_theme.dart';
import 'package:flutter/material.dart';

class CreateLocationScreen extends StatefulWidget {
  const CreateLocationScreen({super.key});

  @override
  State<CreateLocationScreen> createState() => _CreateLocationScreenState();
}

class _CreateLocationScreenState extends State<CreateLocationScreen> {
  // Controllers
  final TextEditingController storeName = TextEditingController();
  final TextEditingController phone = TextEditingController();
  final TextEditingController address1 = TextEditingController();
  final TextEditingController city = TextEditingController();
  final TextEditingController stateCtrl = TextEditingController();
  final TextEditingController postalCode = TextEditingController();
  final TextEditingController country = TextEditingController();
  final TextEditingController timeZone = TextEditingController(
    text: "America/New_York",
  );
  TimeOfDay openTime = const TimeOfDay(hour: 9, minute: 0);
  TimeOfDay closeTime = const TimeOfDay(hour: 18, minute: 0);

  Future<void> pickTime({required bool isOpen}) async {
    final TimeOfDay? picked = await showTimePicker(
      context: context,
      initialTime: isOpen ? openTime : closeTime,
    );

    if (picked != null) {
      setState(() {
        if (isOpen)
          openTime = picked;
        else
          closeTime = picked;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFEFFAFF),
      appBar: AppBar(
        title: const Text(
          "Create New Location",
          style: TextStyle(color: Colors.white),
        ),
        backgroundColor: AppTheme.primary,
        elevation: 0,
        foregroundColor: Colors.black,
      ),

      body: SingleChildScrollView(
        padding: const EdgeInsets.all(20),
        child: Container(
          padding: const EdgeInsets.all(30),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(20),
            boxShadow: [
              BoxShadow(
                color: Colors.black12,
                blurRadius: 20,
                offset: const Offset(0, 10),
              ),
            ],
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _input("Store Name *", "Enter store name", storeName),
              _input("Phone *", "Enter phone number", phone),
              _input("Address Line 1 *", "Enter address", address1),

              Row(
                children: [
                  Expanded(child: _input("City *", "City", city)),
                  const SizedBox(width: 12),
                  Expanded(child: _input("State *", "State", stateCtrl)),
                ],
              ),

              Row(
                children: [
                  Expanded(
                    child: _input("Postal Code *", "Postal Code", postalCode),
                  ),
                  const SizedBox(width: 12),
                  Expanded(
                    child: _input(
                      "Country *",
                      "Country Code (e.g. US)",
                      country,
                    ),
                  ),
                ],
              ),

              _input("Time Zone *", "America/New_York", timeZone),

              const SizedBox(height: 15),

              Row(
                children: [
                  Expanded(
                    child: _timePicker(
                      label: "Open Time *",
                      time: openTime,
                      onTap: () => pickTime(isOpen: true),
                    ),
                  ),
                  const SizedBox(width: 12),
                  Expanded(
                    child: _timePicker(
                      label: "Close Time *",
                      time: closeTime,
                      onTap: () => pickTime(isOpen: false),
                    ),
                  ),
                ],
              ),

              const SizedBox(height: 25),

              SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                  onPressed: () {
                    final json = {
                      "store_name": storeName.text,
                      "phone": phone.text,
                      "address1": address1.text,
                      "city": city.text,
                      "state": stateCtrl.text,
                      "postal_code": postalCode.text,
                      "country": country.text,
                      "timezone": timeZone.text,
                      "open_time": openTime.format(context),
                      "close_time": closeTime.format(context),
                    };

                    print("📦 LOCATION JSON → $json");
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF00CFFF),
                    padding: const EdgeInsets.symmetric(vertical: 15),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                  child: const Text(
                    "Save & Console JSON",
                    style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.w600,
                      fontSize: 16,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  // Reusable TextField
  Widget _input(String label, String hint, TextEditingController controller) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 18),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            label,
            style: const TextStyle(fontWeight: FontWeight.w600, fontSize: 14),
          ),
          const SizedBox(height: 8),
          TextField(
            controller: controller,
            decoration: InputDecoration(
              hintText: hint,
              filled: true,
              fillColor: const Color(0xFFF0F6FF),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(12),
                borderSide: BorderSide.none,
              ),
            ),
          ),
        ],
      ),
    );
  }

  // Time Picker Widget
  Widget _timePicker({
    required String label,
    required TimeOfDay time,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            label,
            style: const TextStyle(fontWeight: FontWeight.w600, fontSize: 14),
          ),
          const SizedBox(height: 8),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 18),
            decoration: BoxDecoration(
              color: const Color(0xFFF0F6FF),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Row(
              children: [
                Text(
                  time.format(context),
                  style: const TextStyle(fontSize: 16),
                ),
                const Spacer(),
                const Icon(Icons.access_time, size: 20),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
