import { createFeature, createReducer, on } from '@ngrx/store';
import { createEntityAdapter, EntityState } from '@ngrx/entity';
import { PaginationMeta } from '../../../models/api.model';
import { PurchaseGrn } from '../../../models/purchase-grn.model';
import { PurchaseGrnsActions } from './grns.actions';

export interface PurchaseGrnsState extends EntityState<PurchaseGrn> {
  meta: PaginationMeta | null;
  loading: boolean;
  saving: boolean;
  error: string | null;
  loaded: boolean;
}

const adapter = createEntityAdapter<PurchaseGrn>();

const initialState: PurchaseGrnsState = adapter.getInitialState({
  meta: null,
  loading: false,
  saving: false,
  error: null,
  loaded: false,
});

export const purchaseGrnsFeature = createFeature({
  name: 'purchaseGrns',
  reducer: createReducer(
    initialState,

    // ─── Load All ──────────────────────────────────────────────
    on(PurchaseGrnsActions.loadGrns, (state, { force }) => ({
      ...state,
      loading: !state.loaded || force === true,
      error: null,
    })),
    on(PurchaseGrnsActions.loadGrnsSuccess, (state, { grns, meta }) =>
      adapter.setAll(grns, { ...state, meta, loading: false, loaded: true })
    ),
    on(PurchaseGrnsActions.loadGrnsFailure, (state, { error }) => ({
      ...state,
      loading: false,
      error,
    })),

    // ─── Load Single ───────────────────────────────────────────
    on(PurchaseGrnsActions.loadGrn, (state) => ({
      ...state,
      loading: true,
      error: null,
    })),
    on(PurchaseGrnsActions.loadGrnSuccess, (state, { grn }) =>
      adapter.upsertOne(grn, { ...state, loading: false })
    ),
    on(PurchaseGrnsActions.loadGrnFailure, (state, { error }) => ({
      ...state,
      loading: false,
      error,
    })),

    // ─── Create ────────────────────────────────────────────────
    on(PurchaseGrnsActions.createGrn, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(PurchaseGrnsActions.createGrnSuccess, (state, { grn }) =>
      adapter.addOne(grn, { ...state, saving: false })
    ),
    on(PurchaseGrnsActions.createGrnFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),

    // ─── Update ────────────────────────────────────────────────
    on(PurchaseGrnsActions.updateGrn, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(PurchaseGrnsActions.updateGrnSuccess, (state, { grn }) =>
      adapter.upsertOne(grn, { ...state, saving: false })
    ),
    on(PurchaseGrnsActions.updateGrnFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),

    // ─── Delete ────────────────────────────────────────────────
    on(PurchaseGrnsActions.deleteGrn, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(PurchaseGrnsActions.deleteGrnSuccess, (state, { id }) =>
      adapter.removeOne(id, { ...state, saving: false })
    ),
    on(PurchaseGrnsActions.deleteGrnFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),

    // ─── Status Changes ───────────────────────────────────────
    on(
      PurchaseGrnsActions.approveGrn,
      PurchaseGrnsActions.cancelGrn,
      (state) => ({ ...state, saving: true, error: null })
    ),
    on(PurchaseGrnsActions.statusChangeSuccess, (state, { grn }) =>
      adapter.upsertOne(grn, { ...state, saving: false })
    ),
    on(PurchaseGrnsActions.statusChangeFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),

    // ─── Clear Error ───────────────────────────────────────────
    on(PurchaseGrnsActions.clearGrnsError, (state) => ({
      ...state,
      error: null,
    }))
  ),
});

const { selectAll, selectEntities, selectTotal } = adapter.getSelectors(
  purchaseGrnsFeature.selectPurchaseGrnsState
);

export const selectAllPurchaseGrns = selectAll;
export const selectPurchaseGrnEntities = selectEntities;
export const selectPurchaseGrnsTotal = selectTotal;
export const selectPurchaseGrnsMeta = purchaseGrnsFeature.selectMeta;
export const selectPurchaseGrnsLoading = purchaseGrnsFeature.selectLoading;
export const selectPurchaseGrnsSaving = purchaseGrnsFeature.selectSaving;
export const selectPurchaseGrnsError = purchaseGrnsFeature.selectError;
export const selectPurchaseGrnsLoaded = purchaseGrnsFeature.selectLoaded;
