import { Injectable } from '@angular/core';
import { Store, StoreConfig } from '@datorama/akita';
import { IAbout } from '../about/about.model';
import { IContactModel } from '../contact/contact.model';
import { IProjects } from '../projects/projects.model';
import { IResume } from '../resume/resume.model';

export type AdminSection = 'about' | 'contact' | 'projects' | 'resume';

export interface AdminState {
  about: IAbout | null;
  contact: IContactModel | null;
  projects: IProjects | null;
  resume: IResume | null;
  loading: Record<AdminSection, boolean>;
  error: Record<AdminSection, string | null>;
}

export function createInitialState(): AdminState {
  return {
    about: null,
    contact: null,
    projects: null,
    resume: null,
    loading: { about: false, contact: false, projects: false, resume: false },
    error: { about: null, contact: null, projects: null, resume: null }
  };
}

@Injectable({ providedIn: 'root' })
@StoreConfig({ name: 'admin', resettable: true })
export class AdminStore extends Store<AdminState> {
  constructor() {
    super(createInitialState());
  }

  setSectionLoading(section: AdminSection, loading: boolean) {
    this.update(state => ({
      loading: { ...state.loading, [section]: loading }
    }));
  }

  setSectionError(section: AdminSection, error: string | null) {
    this.update(state => ({
      error: { ...state.error, [section]: error }
    }));
  }
}
