import { get, post, patch, del } from './client.js';

// Disciplines
export async function fetchDisciplines(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/organization/discipline?${query}` : '/api/organization/discipline');
}

export async function fetchDiscipline(id) {
  return get(`/api/organization/discipline/${id}`);
}

export async function createDiscipline(data) {
  const payload = {
    DisciplineCode: data.DisciplineCode,
    DisciplineName: data.DisciplineName,
    Parent: data.Parent || null,
    SeqScr: data.SeqScr,
    SeqRpt: data.SeqRpt,
  };
  return post('/api/organization/discipline', payload);
}

export async function updateDiscipline(id, data) {
  const payload = {
    DisciplineCode: data.DisciplineCode,
    DisciplineName: data.DisciplineName,
    Parent: data.Parent || null,
    SeqScr: data.SeqScr,
    SeqRpt: data.SeqRpt,
  };
  return patch(`/api/organization/discipline/${id}`, payload);
}

export async function deleteDiscipline(id) {
  return del('/api/organization/discipline', { id });
}

// Departments
export async function fetchDepartments(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/organization/department?${query}` : '/api/organization/department');
}

export async function fetchDepartment(id) {
  return get(`/api/organization/department/${id}`);
}

export async function createDepartment(data) {
  const payload = {
    DeptCode: data.DeptCode,
    DeptName: data.DeptName,
    SiteID: data.SiteID,
  };
  return post('/api/organization/department', payload);
}

export async function updateDepartment(id, data) {
  const payload = {
    DeptCode: data.DeptCode,
    DeptName: data.DeptName,
    SiteID: data.SiteID,
  };
  return patch(`/api/organization/department/${id}`, payload);
}

export async function deleteDepartment(id) {
  return del('/api/organization/department', { id });
}

// Sites
export async function fetchSites(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/organization/site?${query}` : '/api/organization/site');
}

export async function fetchSite(id) {
  return get(`/api/organization/site/${id}`);
}

export async function createSite(data) {
  const payload = {
    SiteCode: data.SiteCode,
    SiteName: data.SiteName,
    AccountID: data.AccountID,
  };
  return post('/api/organization/site', payload);
}

export async function updateSite(id, data) {
  const payload = {
    SiteCode: data.SiteCode,
    SiteName: data.SiteName,
    AccountID: data.AccountID,
  };
  return patch(`/api/organization/site/${id}`, payload);
}

export async function deleteSite(id) {
  return del('/api/organization/site', { id });
}

// Workstations
export async function fetchWorkstations(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/organization/workstation?${query}` : '/api/organization/workstation');
}

export async function fetchWorkstation(id) {
  return get(`/api/organization/workstation/${id}`);
}

// HostApps
export async function fetchHostApps(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/organization/hostapp?${query}` : '/api/organization/hostapp');
}

export async function fetchHostApp(id) {
  return get(`/api/organization/hostapp/${id}`);
}

export async function createHostApp(data) {
  const payload = {
    HostAppName: data.HostAppName,
    SiteID: data.SiteID,
  };
  return post('/api/organization/hostapp', payload);
}

export async function updateHostApp(id, data) {
  const payload = {
    HostAppName: data.HostAppName,
    SiteID: data.SiteID,
  };
  return patch(`/api/organization/hostapp/${id}`, payload);
}

export async function deleteHostApp(id) {
  return del('/api/organization/hostapp', { id });
}

// HostComParas
export async function fetchHostComParas(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/organization/hostcompara?${query}` : '/api/organization/hostcompara');
}

export async function fetchHostComPara(id) {
  return get(`/api/organization/hostcompara/${id}`);
}

export async function createHostComPara(data) {
  const payload = {
    HostAppID: data.HostAppID,
    HostIP: data.HostIP,
    HostPort: data.HostPort,
    HostPwd: data.HostPwd,
  };
  return post('/api/organization/hostcompara', payload);
}

export async function updateHostComPara(id, data) {
  const payload = {
    HostAppID: data.HostAppID,
    HostIP: data.HostIP,
    HostPort: data.HostPort,
    HostPwd: data.HostPwd,
  };
  return patch(`/api/organization/hostcompara/${id}`, payload);
}

export async function deleteHostComPara(id) {
  return del('/api/organization/hostcompara', { id });
}

// CodingSystems
export async function fetchCodingSystems(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/organization/codingsys?${query}` : '/api/organization/codingsys');
}

export async function fetchCodingSystem(id) {
  return get(`/api/organization/codingsys/${id}`);
}

export async function createCodingSystem(data) {
  const payload = {
    CodingSysAbb: data.CodingSysAbb,
    FullText: data.FullText,
    Description: data.Description,
  };
  return post('/api/organization/codingsys', payload);
}

export async function updateCodingSystem(id, data) {
  const payload = {
    CodingSysAbb: data.CodingSysAbb,
    FullText: data.FullText,
    Description: data.Description,
  };
  return patch(`/api/organization/codingsys/${id}`, payload);
}

export async function deleteCodingSystem(id) {
  return del('/api/organization/codingsys', { id });
}


// Accounts
export async function fetchAccounts(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/organization/account?${query}` : '/api/organization/account');
}

export async function fetchAccount(id) {
  return get(`/api/organization/account/${id}`);
}

export async function createAccount(data) {
  const payload = {
    AccountName: data.AccountName,
    Initial: data.Initial,
    Parent: data.Parent,
  };
  return post('/api/organization/account', payload);
}

export async function updateAccount(id, data) {
  const payload = {
    AccountName: data.AccountName,
    Initial: data.Initial,
    Parent: data.Parent,
  };
  return patch(`/api/organization/account/${id}`, payload);
}

export async function deleteAccount(id) {
  return del('/api/organization/account', { id });
}

// Workstations
export async function createWorkstation(data) {
  const payload = {
    WorkstationCode: data.WorkstationCode,
    WorkstationName: data.WorkstationName,
    SiteID: data.SiteID,
    DepartmentID: data.DepartmentID,
  };
  return post('/api/organization/workstation', payload);
}

export async function updateWorkstation(id, data) {
  const payload = {
    WorkstationCode: data.WorkstationCode,
    WorkstationName: data.WorkstationName,
    SiteID: data.SiteID,
    DepartmentID: data.DepartmentID,
  };
  return patch(`/api/organization/workstation/${id}`, payload);
}

export async function deleteWorkstation(id) {
  return del('/api/organization/workstation', { id });
}
