import 'package:flutter/material.dart';
import '../models/recurring_block_model.dart';

class RecurringBlockViewModel extends ChangeNotifier {
  final RecurringBlockModel _model = RecurringBlockModel();

  int _currentStep = 0;
  String? _selectedCategory;
  String? _selectedRecurrence;
  Set<String> _selectedDays = {};
  int _repeatWeek = 1;
  DateTime _focusedMonth = DateTime.now();
  Set<int> _selectedDates = {};
  int _capacity = 50;
  List<TimeSlot> _timeSlots = [];

  // ---------- Getters ----------
  int get currentStep => _currentStep;
  String? get selectedCategory => _selectedCategory;
  String? get selectedRecurrence => _selectedRecurrence;
  Set<String> get selectedDays => _selectedDays;
  int get repeatWeek => _repeatWeek;
  DateTime get focusedMonth => _focusedMonth;
  Set<int> get selectedDates => _selectedDates;
  int get capacity => _capacity;
  List<TimeSlot> get timeSlots => _timeSlots;
  RecurringBlockModel get model => _model;

  // ---------- Step Navigation ----------
  void nextStep() {
    if (_currentStep < 3) {
      _currentStep++;
      notifyListeners();
    }
  }

  void previousStep(BuildContext context) {
    if (_currentStep > 0) {
      _currentStep--;
      notifyListeners();
    } else {
      Navigator.pop(context);
    }
  }

  // ---------- Category ----------
  void selectCategory(String category) {
    _selectedCategory = category;
    notifyListeners();
  }

  // ---------- Recurrence Type ----------
  void selectRecurrence(String type) {
    // ✅ Single-choice only (cannot deselect, always one active)
    if (_selectedRecurrence != type) {
      _selectedRecurrence = type;
      notifyListeners();
    }
  }

  // ---------- Weekly Day Selection ----------
  void toggleDay(String day) {
    if (_selectedDays.contains(day)) {
      _selectedDays.remove(day);
    } else {
      _selectedDays.add(day);
    }
    notifyListeners();
  }

  // ---------- Week Repeat Increment/Decrement ----------
  void incrementWeek() {
    _repeatWeek++;
    notifyListeners();
  }

  void decrementWeek() {
    if (_repeatWeek > 1) {
      _repeatWeek--;
      notifyListeners();
    }
  }

  // ---------- Month Navigation ----------
  void changeMonth(int offset) {
    _focusedMonth = DateTime(
      _focusedMonth.year,
      _focusedMonth.month + offset,
    );
    notifyListeners();
  }

  // ---------- Monthly Date Selection ----------
  void toggleDate(int day) {
    if (_selectedDates.contains(day)) {
      _selectedDates.remove(day);
    } else {
      _selectedDates.add(day);
    }
    notifyListeners();
  }

  // ---------- Time Slots ----------
  void addTimeSlot(String start, String end, int capacity) {
    _timeSlots.add(TimeSlot(start, end, capacity));
    notifyListeners();
  }

  void clearSlots() {
    _timeSlots.clear();
    notifyListeners();
  }

  // ---------- Capacity ----------
  void incrementCapacity() {
    _capacity++;
    notifyListeners();
  }

  void decrementCapacity() {
    if (_capacity > 1) _capacity--;
    notifyListeners();
  }

  // ---------- Model Field Updates ----------
  void updateAttractionName(String value) {
    _model.attractionName = value;
    notifyListeners();
  }

  void updateLocation(String value) {
    _model.location = value;
    notifyListeners();
  }

  void updateDescription(String value) {
    _model.description = value;
    notifyListeners();
  }

  void updateStartDate(DateTime date) {
    _model.startDate = date;
    notifyListeners();
  }

  void updateEndDate(DateTime date) {
    _model.endDate = date;
    notifyListeners();
  }
}
