/*
 * File: authSlice.ts
 * Description: Redux slice for Auth state management
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */

import { createSlice, PayloadAction } from '@reduxjs/toolkit';

// Sample user type

interface NotificationPreferences {
  critical_alerts: {
    email: boolean;
    in_app: boolean;
    push: boolean;
    sms: boolean;
  };
  system_notifications: {
    push: boolean;
    email: boolean;
    in_app: boolean;
  };
}

interface DashboardSettings {
  theme: string;
  language: string;
  timezone: string;
}

interface UserProfile {
  accent_color: string | null;
  dashboard_role: string;
  dashboard_settings: DashboardSettings;
  display_name: string;
  email: string;
  first_name: string;
  last_name: string;
  hospital_id: string;
  notification_preferences: NotificationPreferences;
  onboarded: boolean;
  onboarding_completed: boolean;
  onboarding_message: string;
  onboarding_step: number;
  profile_photo_url: string | null;
  theme_color: string | null;
  user_id: string;
  access_token: string;
}

// Auth state type
interface AuthState {
  user: UserProfile | null;
  loading: boolean;
  error: string | null;
  isAuthenticated: boolean;
}

const initialState: AuthState = {
  user: null,
  loading: false,
  error: null,
  isAuthenticated: false,
};

/**
 * Auth slice for managing authentication state
 */
const authSlice = createSlice({
  name: 'auth',
  initialState,
  reducers: {
    loginStart(state) {
      state.loading = true;
      state.error = null;
    },
    loginSuccess(state, action: PayloadAction<UserProfile>) {
      state.user = action.payload;
      state.isAuthenticated = true;
      state.loading = false;
    },
    loginFailure(state, action: PayloadAction<string>) {
      state.loading = false;
      state.error = action.payload;
      state.isAuthenticated = false;
    },
    logout(state) {
      state.user = null;
      state.isAuthenticated = false;
    },
  },
});

export const {
  loginStart,
  loginSuccess,
  loginFailure,
  logout,
} = authSlice.actions;

export default authSlice.reducer;

/*
 * End of File: authSlice.ts
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */ 