import 'package:flutter/material.dart';
import '../../../core/app_theme.dart';
import '../providers/control_state.dart';
import 'schedule_item.dart';

class ScheduleSection extends StatelessWidget {
  final List<WateringSchedule> schedules;
  final VoidCallback onAddSchedule;
  final void Function(String id, bool value) onToggleSchedule;
  final void Function(String id) onRemoveSchedule;

  const ScheduleSection({
    super.key,
    required this.schedules,
    required this.onAddSchedule,
    required this.onToggleSchedule,
    required this.onRemoveSchedule,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: AppTheme.cardDecoration(),
      child: Column(
        children: [
          _Header(onAdd: onAddSchedule),
          if (schedules.isEmpty)
            const Padding(
              padding: EdgeInsets.all(24),
              child: Text(
                'Belum ada jadwal. Tap + untuk menambah.',
                style: TextStyle(
                    color: AppTheme.textHint, fontSize: 13),
              ),
            )
          else
            ListView.separated(
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              itemCount: schedules.length,
              separatorBuilder: (context, index) => Divider(
                height: 1,
                color: Colors.grey.shade200,
                indent: 16,
                endIndent: 16,
              ),
              itemBuilder: (_, i) {
                final s = schedules[i];
                return ScheduleItem(
                  id: s.id,
                  time: s.formattedTime,
                  repeat: s.repeat,
                  isEnabled: s.isEnabled,
                  onToggle: onToggleSchedule,
                  onRemove: onRemoveSchedule,
                );
              },
            ),
        ],
      ),
    );
  }
}

class _Header extends StatelessWidget {
  final VoidCallback onAdd;
  const _Header({required this.onAdd});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
      decoration: const BoxDecoration(
        color: AppTheme.accent,
        borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
      ),
      child: Row(
        children: [
          const Icon(Icons.calendar_month, color: Colors.white, size: 20),
          const SizedBox(width: 10),
          const Text(
            'Jadwal Penyiraman',
            style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.w700,
              fontSize: 16,
            ),
          ),
          const Spacer(),
          GestureDetector(
            onTap: onAdd,
            child: Container(
              width: 28,
              height: 28,
              decoration: BoxDecoration(
                color: Colors.white.withOpacity(0.25),
                shape: BoxShape.circle,
              ),
              child: const Icon(Icons.add, color: Colors.white, size: 18),
            ),
          ),
        ],
      ),
    );
  }
}
