import { inject, Injectable } from '@angular/core';
import { IAbout } from '../about/about.model';
import { IContactModel } from '../contact/contact.model';
import { IResume } from '../resume/resume.model';
import { IProjects } from '../projects/projects.model';
import { IProject } from '../models/project.model';
import { IHobby } from '../models/hobby.model';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../../../environments/environment';

@Injectable({
  providedIn: 'root'
})
export class AdminService {
  private readonly baseUrl = (environment.apiUrl ?? '').replace(/\/+$/, '');
  private http = inject(HttpClient);

  private api(path: string) {
    return `${this.baseUrl}${path.startsWith('/') ? '' : '/'}${path}`;
  }

  getHobbies(): Observable<IAbout> {
    return this.http.get<IAbout>(this.api('/api/v1/admin/GetHobbies'));
  }

  getCandidateWithSocialLinks(): Observable<IContactModel> {
    return this.http.get<IContactModel>(this.api('/api/v1/admin/GetCandidateWithSocialLinks'));
  }

  getResume(): Observable<IResume> {
    return this.http.get<IResume>(this.api('/api/v1/admin/GetResume'));
  }

  getProjects(): Observable<IProjects> {
    return this.http.get<IProjects>(this.api('/api/v1/admin/GetProjects'));
  }

  upsertProject(project: IProject): Observable<IProject> {
    return this.http.post<IProject>(this.api('/api/v1/admin/UpsertProject'), project);
  }

  deleteProject(projectId: number): Observable<void> {
    return this.http.delete<void>(this.api(`/api/v1/admin/DeleteProject/${projectId}`));
  }

  upsertHobbies(hobbies: IHobby[]): Observable<IHobby[]> {
    return this.http.post<IHobby[]>(this.api('/api/v1/admin/UpsertHobbies'), hobbies);
  }
}
