2026-04-20 23:52:00 +02:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
|
import 'package:flux/core/blocs/session/session_cubit.dart';
|
|
|
|
|
import 'package:flux/core/data/core_repository.dart';
|
|
|
|
|
import 'package:flux/features/company/models/company_model.dart';
|
|
|
|
|
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
|
|
|
|
|
import 'package:flux/features/master_data/store/models/store_model.dart';
|
|
|
|
|
import 'package:flux/features/onboarding/blocs/onboarding_state.dart';
|
2026-04-21 19:16:41 +02:00
|
|
|
import 'package:get_it/get_it.dart';
|
|
|
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
2026-04-20 23:52:00 +02:00
|
|
|
|
|
|
|
|
class OnboardingCubit extends Cubit<OnboardingState> {
|
|
|
|
|
final CoreRepository _repository;
|
|
|
|
|
final SessionCubit _sessionCubit;
|
|
|
|
|
|
|
|
|
|
OnboardingCubit(this._sessionCubit, this._repository)
|
2026-04-22 11:05:01 +02:00
|
|
|
: super(OnboardingState(
|
|
|
|
|
step: _sessionCubit.state.onboardingStep,
|
|
|
|
|
companyId: _sessionCubit.state.company?.id,
|
|
|
|
|
storeId: _sessionCubit.state.currentStore?.id,
|
|
|
|
|
));
|
2026-04-20 23:52:00 +02:00
|
|
|
|
|
|
|
|
// --- STEP 1: REGISTRAZIONE AZIENDA ---
|
2026-04-21 19:16:41 +02:00
|
|
|
Future<void> saveCompany(String companyName) async {
|
2026-04-20 23:52:00 +02:00
|
|
|
emit(state.copyWith(isLoading: true));
|
2026-04-21 19:16:41 +02:00
|
|
|
final company = CompanyModel.empty().copyWith(
|
|
|
|
|
ragioneSociale: companyName,
|
|
|
|
|
userId: GetIt.I<SupabaseClient>().auth.currentUser!.id,
|
|
|
|
|
subscriptionTier: SubscriptionTier.pro,
|
|
|
|
|
subscriptionStatus: SubscriptionStatus.trialing,
|
|
|
|
|
trialEndsAt: DateTime.now().add(const Duration(days: 14)),
|
|
|
|
|
);
|
2026-04-20 23:52:00 +02:00
|
|
|
try {
|
|
|
|
|
// Il repository restituisce il modello creato con l'ID di Supabase
|
|
|
|
|
final savedCompany = await _repository.createCompany(company);
|
|
|
|
|
|
|
|
|
|
emit(
|
|
|
|
|
state.copyWith(
|
|
|
|
|
isLoading: false,
|
|
|
|
|
step: OnboardingStep.store,
|
|
|
|
|
companyId: savedCompany.id,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
emit(
|
|
|
|
|
state.copyWith(
|
|
|
|
|
isLoading: false,
|
|
|
|
|
error: "Errore salvataggio azienda: $e",
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- STEP 2: REGISTRAZIONE PRIMO NEGOZIO ---
|
|
|
|
|
Future<void> saveStore(StoreModel store) async {
|
|
|
|
|
if (state.companyId == null) return;
|
2026-04-22 11:05:01 +02:00
|
|
|
if (state.companyId == '') return;
|
2026-04-20 23:52:00 +02:00
|
|
|
|
|
|
|
|
emit(state.copyWith(isLoading: true));
|
|
|
|
|
try {
|
|
|
|
|
// Iniettiamo forzatamente il companyId ottenuto dallo step precedente
|
|
|
|
|
final storeToSave = store.copyWith(companyId: state.companyId);
|
|
|
|
|
final savedStore = await _repository.createStore(storeToSave);
|
2026-04-22 11:05:01 +02:00
|
|
|
_sessionCubit.changeStore(savedStore);
|
2026-04-20 23:52:00 +02:00
|
|
|
|
|
|
|
|
emit(
|
|
|
|
|
state.copyWith(
|
|
|
|
|
isLoading: false,
|
|
|
|
|
step: OnboardingStep.staff,
|
|
|
|
|
storeId: savedStore.id,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
emit(
|
|
|
|
|
state.copyWith(isLoading: false, error: "Errore salvataggio store: $e"),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- STEP 3: REGISTRAZIONE PROFILO STAFF (PAZIENTE ZERO) ---
|
|
|
|
|
Future<void> saveStaff(StaffMemberModel staff) async {
|
2026-04-22 11:05:01 +02:00
|
|
|
if (state.companyId == null) return;
|
|
|
|
|
if (state.companyId == '') return;
|
2026-04-20 23:52:00 +02:00
|
|
|
|
|
|
|
|
emit(state.copyWith(isLoading: true));
|
|
|
|
|
try {
|
|
|
|
|
// PARANOIA MODE: Forziamo i legami e il ruolo di sistema 'admin'
|
|
|
|
|
final staffToSave = staff.copyWith(
|
|
|
|
|
companyId: state.companyId!,
|
|
|
|
|
userId: _sessionCubit.state.user!.id, // Dall'utente loggato in Supabase
|
|
|
|
|
systemRole: SystemRole.admin, // Blindato!
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await _repository.createStaffMember(staffToSave);
|
|
|
|
|
|
|
|
|
|
emit(state.copyWith(isLoading: false, step: OnboardingStep.completed));
|
|
|
|
|
|
|
|
|
|
// Svegliamo il SessionCubit: lui ricalcolerà tutto e aprirà la Dashboard
|
|
|
|
|
await _sessionCubit.initializeSession();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
emit(
|
|
|
|
|
state.copyWith(isLoading: false, error: "Errore creazione profilo: $e"),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|