import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:on_field_work/controller/directory/create_bucket_controller.dart';
import 'package:on_field_work/helpers/utils/base_bottom_sheet.dart';
import 'package:on_field_work/helpers/widgets/my_text.dart';
import 'package:on_field_work/helpers/widgets/my_spacing.dart';
import 'package:on_field_work/helpers/widgets/my_text_style.dart';

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

  @override
  State<CreateBucketBottomSheet> createState() => _CreateBucketBottomSheetState();
}

class _CreateBucketBottomSheetState extends State<CreateBucketBottomSheet> {
  final BucketController _controller = Get.put(BucketController());
  final _formKey = GlobalKey<FormState>();

  InputDecoration _inputDecoration(String hint) {
    return InputDecoration(
      hintText: hint,
      hintStyle: MyTextStyle.bodySmall(xMuted: true),
      filled: true,
      fillColor: Colors.grey.shade100,
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(12),
        borderSide: BorderSide(color: Colors.grey.shade300),
      ),
      enabledBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(12),
        borderSide: BorderSide(color: Colors.grey.shade300),
      ),
      focusedBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(12),
        borderSide: BorderSide(color: Colors.blueAccent, width: 1.5),
      ),
      contentPadding: MySpacing.all(16),
    );
  }

  Widget _formContent() {
    return Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          MyText.labelMedium("Bucket Name"),
          MySpacing.height(8),
          TextFormField(
            initialValue: _controller.name.value,
            onChanged: _controller.updateName,
            validator: (value) {
              if (value == null || value.trim().isEmpty) {
                return "Bucket name is required";
              }
              return null;
            },
            decoration: _inputDecoration("e.g., Project Docs"),
          ),
          MySpacing.height(16),
          MyText.labelMedium("Description"),
          MySpacing.height(8),
          TextFormField(
            initialValue: _controller.description.value,
            onChanged: _controller.updateDescription,
            maxLines: 3,
            decoration: _inputDecoration("Optional bucket description"),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BucketController>(
      builder: (_) {
        return SafeArea(
          top: false,
          child: BaseBottomSheet(
            title: "Create New Bucket",
            child: _formContent(),
            onCancel: () => Navigator.pop(context, false),
            onSubmit: () async {
              if (_formKey.currentState!.validate()) {
                await _controller.createBucket();
              }
            },
            isSubmitting: _controller.isCreating.value,
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    Get.delete<BucketController>();
    super.dispose();
  }
}
