import 'dart:io';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:image_picker/image_picker.dart'; // Import ImagePicker
import '../../repository/raise_ticket_repository.dart';

part 'raise_ticket_event.dart';
part 'raise_ticket_state.dart';

class RaiseTicketBloc extends Bloc<RaiseTicketEvent, RaiseTicketState> {
  final RaiseTicketRepository _raiseTicketRepository;
  final ImagePicker _picker = ImagePicker(); // Initialize ImagePicker
  static const int _maxFileSizeInBytes = 5 * 1024 * 1024; // 5 MB limit

  RaiseTicketBloc({RaiseTicketRepository? raiseTicketRepository})
      : _raiseTicketRepository =
            raiseTicketRepository ?? RaiseTicketRepository(),
        super(const RaiseTicketState()) {
    on<RaiseTicketSubjectChanged>(_onSubjectChanged);
    on<RaiseTicketDescriptionChanged>(_onDescriptionChanged);
    on<RaiseTicketAttachmentPicked>(_onAttachmentPicked);
    on<RaiseTicketAttachmentRemoved>(_onAttachmentRemoved);
    on<RaiseTicketSubmitted>(_onRaiseTicketSubmitted);
    on<RaiseTicketReset>(_onReset);
  }

  void _onSubjectChanged(
      RaiseTicketSubjectChanged event, Emitter<RaiseTicketState> emit) {
    emit(state.copyWith(subject: event.subject));
  }

  void _onDescriptionChanged(
      RaiseTicketDescriptionChanged event, Emitter<RaiseTicketState> emit) {
    emit(state.copyWith(description: event.description));
  }

  Future<void> _onAttachmentPicked(
      RaiseTicketAttachmentPicked event, Emitter<RaiseTicketState> emit) async {
    // The event now passes the File directly after it's picked in the UI
    final File file = event.attachment;
    final int fileSize = await file.length();

    if (fileSize > _maxFileSizeInBytes) {
      emit(state.copyWith(
        attachment: null,
        fileSizeExceeded: true,
        errorMessage: "File size exceeds 5MB limit.",
      ));
    } else {
      emit(state.copyWith(
        attachment: file,
        fileSizeExceeded: false,
        errorMessage: null,
      ));
    }
  }

  void _onAttachmentRemoved(
      RaiseTicketAttachmentRemoved event, Emitter<RaiseTicketState> emit) {
    emit(state.copyWith(
      clearAttachment: true,
      fileSizeExceeded: false,
      errorMessage: null,
    ));
  }

  Future<void> _onRaiseTicketSubmitted(
      RaiseTicketSubmitted event, Emitter<RaiseTicketState> emit) async {
    if (state.subject.isEmpty || state.description.isEmpty) {
      emit(state.copyWith(
          errorMessage: "Subject and description cannot be empty."));
      return;
    }
    if (state.fileSizeExceeded) {
      emit(state.copyWith(errorMessage: "Cannot submit: file size exceeded."));
      return;
    }

    emit(state.copyWith(status: RaiseTicketStatus.loading, errorMessage: null));
    try {
      await _raiseTicketRepository.raiseTicket(
        subject: state.subject,
        description: state.description,
        attachment: state.attachment,
      );
      emit(state.copyWith(status: RaiseTicketStatus.success));
    } catch (e) {
      emit(state.copyWith(
        status: RaiseTicketStatus.failure,
        errorMessage: e.toString(),
      ));
    }
  }

  void _onReset(
      RaiseTicketReset event, Emitter<RaiseTicketState> emit) {
    emit(const RaiseTicketState());
  }
}