/**
 * Shinkan Jinkendo API Client
 *
 * Zentrale API-Kommunikation mit automatischer Token-Injektion
 */

import { request, ACTIVE_CLUB_STORAGE_KEY } from '../api/client.js'
import * as exercises from '../api/exercises.js'
import * as planning from '../api/planning.js'

export { ACTIVE_CLUB_STORAGE_KEY }
export * from '../api/exercises.js'
export * from '../api/planning.js'

// ============================================================================
// Auth
// ============================================================================

export async function login(email, password) {
  return request('/api/auth/login', {
    method: 'POST',
    body: JSON.stringify({ email, password })
  })
}

export async function register(email, password, name, extra = {}) {
  return request('/api/auth/register', {
    method: 'POST',
    body: JSON.stringify({ email, password, name, ...extra }),
  })
}

export async function logout() {
  return request('/api/auth/logout', { method: 'POST' })
}

export async function getCurrentProfile() {
  return request('/api/profiles/me')
}

/** Liste aller Profile – nur für Plattform-Admins (Vereinsanlage). */
export async function listProfiles() {
  return request('/api/profiles')
}

/** Alle Nutzer inkl. Vereinsmitgliedschaften — nur Portal-Admin (UI: Admin → Nutzer). */
export async function listAdminUsers() {
  return request('/api/admin/users')
}

/** Medien-Speicher (MEDIA_ROOT + relativer Unterordner) — GET: admin/superadmin, PUT: nur superadmin. */
export async function getPlatformMediaStorage() {
  return request('/api/admin/platform-media-storage')
}

export async function putPlatformMediaStorage(payload) {
  return request('/api/admin/platform-media-storage', {
    method: 'PUT',
    body: JSON.stringify(payload),
  })
}

export async function updateProfile(profileId, data) {
  return request(`/api/profiles/${profileId}`, {
    method: 'PUT',
    body: JSON.stringify(data),
  })
}

/**
 * Passwort anderer Konten: Standard leerer Body → E-Mail mit Reset-Link (wie Passwort vergessen).
 * Nur Super-Admins dürfen `newPassword` setzen (direktes Überschreiben des Passwort-Hashes).
 */
export async function managementPasswordReset(profileId, newPassword = null) {
  const body = {}
  if (newPassword != null && String(newPassword).trim() !== '') {
    body.new_password = newPassword
  }
  return request(`/api/profiles/${profileId}/management-password-reset`, {
    method: 'POST',
    body: JSON.stringify(body),
  })
}

export async function changePassword(newPassword) {
  return request('/api/auth/pin', {
    method: 'PUT',
    body: JSON.stringify({ pin: newPassword }),
  })
}

/** GET /api/auth/verify/{token} — keine Auth nötig; Token gehört zur URL des Bestätigungslinks */
export async function verifyEmail(token) {
  const t = encodeURIComponent(token)
  return request(`/api/auth/verify/${t}`, {
    method: 'GET',
  })
}

export async function resendVerification(email) {
  return request('/api/auth/resend-verification', {
    method: 'POST',
    body: JSON.stringify({ email }),
  })
}

// ============================================================================
// Clubs & Groups
// ============================================================================

export async function listClubs() {
  return request('/api/clubs')
}

export async function getClub(id) {
  return request(`/api/clubs/${id}`)
}

export async function createClub(data) {
  return request('/api/clubs', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateClub(id, data) {
  return request(`/api/clubs/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteClub(id) {
  return request(`/api/clubs/${id}`, { method: 'DELETE' })
}

/** Vereinsmitglieder (API für Admin ohne eigene UI) */
export async function listClubMembers(clubId, { includeInactive = false } = {}) {
  const q = includeInactive ? '?include_inactive=true' : ''
  return request(`/api/clubs/${clubId}/members${q}`)
}

export async function getClubMember(clubId, profileId) {
  return request(`/api/clubs/${clubId}/members/${profileId}`)
}

export async function addClubMember(clubId, payload) {
  return request(`/api/clubs/${clubId}/members`, {
    method: 'POST',
    body: JSON.stringify(payload),
  })
}

export async function updateClubMember(clubId, profileId, payload) {
  return request(`/api/clubs/${clubId}/members/${profileId}`, {
    method: 'PUT',
    body: JSON.stringify(payload),
  })
}

export async function removeClubMember(clubId, profileId) {
  return request(`/api/clubs/${clubId}/members/${profileId}`, { method: 'DELETE' })
}

/** Aktive Vereine (öffentlich, für Registrierungswahl). */
export async function listPublicClubsDirectory() {
  return request('/api/clubs/public-directory')
}

/** Vereinsinternes Mitgliederverzeichnis (Trainer-/Co-Auswahl). */
export async function clubMembersDirectory(clubId) {
  return request(`/api/clubs/${clubId}/members/directory`)
}

/** Eigene Beitrittsanträge. */
export async function getMyClubJoinRequests() {
  return request('/api/me/club-join-requests')
}

export async function createClubJoinRequest(payload) {
  return request('/api/me/club-join-requests', {
    method: 'POST',
    body: JSON.stringify(payload),
  })
}

export async function withdrawClubJoinRequest(requestId) {
  return request(`/api/me/club-join-requests/${requestId}`, { method: 'DELETE' })
}

/** Offene Anträge (Vereins-/Plattform-Admin). */
export async function listClubJoinRequests(clubId) {
  return request(`/api/clubs/${clubId}/join-requests`)
}

export async function acceptClubJoinRequest(clubId, requestId, roles = ['trainer']) {
  return request(`/api/clubs/${clubId}/join-requests/${requestId}/accept`, {
    method: 'POST',
    body: JSON.stringify({ roles }),
  })
}

export async function rejectClubJoinRequest(clubId, requestId) {
  return request(`/api/clubs/${clubId}/join-requests/${requestId}/reject`, {
    method: 'POST',
    body: JSON.stringify({}),
  })
}

/** Aggregierter Posteingang: offene Beitrittsanträge für Vereins-/Plattform-Admins. */
export async function getInboxJoinRequests() {
  return request('/api/me/inbox/join-requests')
}

export async function listDivisions(clubId) {
  const query = clubId ? `?club_id=${clubId}` : ''
  return request(`/api/divisions${query}`)
}

export async function createDivision(data) {
  return request('/api/divisions', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateDivision(id, data) {
  return request(`/api/divisions/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteDivision(id) {
  return request(`/api/divisions/${id}`, { method: 'DELETE' })
}

export async function listTrainingGroups(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/groups${query ? '?' + query : ''}`)
}

export async function getTrainingGroup(id) {
  return request(`/api/groups/${id}`)
}

export async function createTrainingGroup(data) {
  return request('/api/groups', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateTrainingGroup(id, data) {
  return request(`/api/groups/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteTrainingGroup(id) {
  return request(`/api/groups/${id}`, { method: 'DELETE' })
}

// ============================================================================
// Skills & Methods
// ============================================================================

export async function listSkills(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/skills${query ? '?' + query : ''}`)
}

/** Admin: Fähigkeiten hierarchisch sortiert (Hauptkategorie → Kategorie → Sortierung) */
export async function listSkillsCatalog(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/skills/catalog${query ? '?' + query : ''}`)
}

export async function getSkill(id) {
  return request(`/api/skills/${id}`)
}

export async function createSkill(data) {
  return request('/api/skills', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateSkill(id, data) {
  return request(`/api/skills/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteSkill(id) {
  return request(`/api/skills/${id}`, { method: 'DELETE' })
}

export async function listMethods(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/methods${query ? '?' + query : ''}`)
}

export async function getMethod(id) {
  return request(`/api/methods/${id}`)
}

export async function createMethod(data) {
  return request('/api/methods', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateMethod(id, data) {
  return request(`/api/methods/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteMethod(id) {
  return request(`/api/methods/${id}`, { method: 'DELETE' })
}

// ============================================================================
// Catalogs (Admin-verwaltbare Stammdaten)
// ============================================================================

// Focus Areas
export async function listFocusAreas(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/focus-areas${query ? '?' + query : ''}`)
}

export async function createFocusArea(data) {
  return request('/api/focus-areas', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateFocusArea(id, data) {
  return request(`/api/focus-areas/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteFocusArea(id) {
  return request(`/api/focus-areas/${id}`, { method: 'DELETE' })
}

// Admin Hierarchy (Tree View)
export async function getAdminHierarchy() {
  return request('/api/admin/hierarchy')
}

// ============================================================================
// Reifegradmodelle / Fähigkeitsmatrix
// ============================================================================

export async function listMaturityModels(filters = {}) {
  const query = new URLSearchParams(
    Object.entries(filters).filter(([, v]) => v !== undefined && v !== null && v !== '')
  ).toString()
  return request(`/api/maturity-models${query ? '?' + query : ''}`)
}

export async function resolveMaturityModel(filters = {}) {
  const query = new URLSearchParams(
    Object.entries(filters).filter(([, v]) => v !== undefined && v !== null && v !== '')
  ).toString()
  return request(`/api/maturity-models/resolve${query ? '?' + query : ''}`)
}

export async function getMaturityModel(id) {
  return request(`/api/maturity-models/${id}`)
}

export async function createMaturityModel(data) {
  return request('/api/maturity-models', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateMaturityModel(id, data) {
  return request(`/api/maturity-models/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteMaturityModel(id) {
  return request(`/api/maturity-models/${id}`, { method: 'DELETE' })
}

export async function addMaturityModelSkill(modelId, data) {
  return request(`/api/maturity-models/${modelId}/skills`, {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function removeMaturityModelSkill(modelId, skillId) {
  return request(`/api/maturity-models/${modelId}/skills/${skillId}`, { method: 'DELETE' })
}

export async function upsertMaturityModelSkillLevels(modelId, data) {
  return request(`/api/maturity-models/${modelId}/skill-levels`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

/** Hierarchische Zuordnung Modell → Fokus / Stilrichtung / Trainingsstil (training_types) */
export async function listMaturityModelContextBindings() {
  return request('/api/maturity-model-context-bindings')
}

export async function upsertMaturityModelContextBinding(data) {
  return request('/api/maturity-model-context-bindings', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function deleteMaturityModelContextBinding(id) {
  return request(`/api/maturity-model-context-bindings/${id}`, { method: 'DELETE' })
}

export async function importMaturityModelBundle(payload) {
  return request('/api/maturity-models/import', {
    method: 'POST',
    body: JSON.stringify(payload)
  })
}

export async function exportMaturityModelBundle(modelId) {
  return request(`/api/maturity-models/${modelId}/export`)
}

export async function exportResolvedMaturityBundle(filters = {}) {
  const query = new URLSearchParams(
    Object.entries(filters).filter(([, v]) => v !== undefined && v !== null && v !== '')
  ).toString()
  return request(`/api/maturity-models/export-resolved${query ? '?' + query : ''}`)
}

/** Komplett: Fähigkeitskatalog + Reifegradmodelle + Kontext-Bindings (slug-/namensbasiert auf Ziel-DB) */
export async function exportMatrixStackBundle() {
  return request('/api/admin/matrix-stack/export')
}

export async function importMatrixStackBundle(payload) {
  return request('/api/admin/matrix-stack/import', {
    method: 'POST',
    body: JSON.stringify(payload)
  })
}

// Style Directions (formerly Training Styles)
export async function listStyleDirections(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/training-styles${query ? '?' + query : ''}`)
}

// Backward-compatibility alias
export const listTrainingStyles = listStyleDirections

export async function createStyleDirection(data) {
  return request('/api/training-styles', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateStyleDirection(id, data) {
  return request(`/api/training-styles/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteStyleDirection(id) {
  return request(`/api/training-styles/${id}`, { method: 'DELETE' })
}

// Training Characters
export async function listTrainingCharacters(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/training-characters${query ? '?' + query : ''}`)
}

export async function createTrainingCharacter(data) {
  return request('/api/training-characters', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateTrainingCharacter(id, data) {
  return request(`/api/training-characters/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteTrainingCharacter(id) {
  return request(`/api/training-characters/${id}`, { method: 'DELETE' })
}

// Training Types (Breitensport, Leistungssport, etc.)
export async function listTrainingTypes(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/training-types${query ? '?' + query : ''}`)
}

export async function createTrainingType(data) {
  return request('/api/training-types', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateTrainingType(id, data) {
  return request(`/api/training-types/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteTrainingType(id) {
  return request(`/api/training-types/${id}`, { method: 'DELETE' })
}

// Skill main categories (Hauptgruppen im Fähigkeitskatalog)
export async function listSkillMainCategories() {
  return request('/api/skill-main-categories')
}

export async function createSkillMainCategory(data) {
  return request('/api/skill-main-categories', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateSkillMainCategory(id, data) {
  return request(`/api/skill-main-categories/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteSkillMainCategory(id) {
  return request(`/api/skill-main-categories/${id}`, { method: 'DELETE' })
}

// Skill Categories
export async function listSkillCategories(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/skill-categories${query ? '?' + query : ''}`)
}

export async function createSkillCategory(data) {
  return request('/api/skill-categories', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateSkillCategory(id, data) {
  return request(`/api/skill-categories/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteSkillCategory(id) {
  return request(`/api/skill-categories/${id}`, { method: 'DELETE' })
}

// Trainer Focus Areas
export async function listTrainerFocusAreas(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/trainer-focus-areas${query ? '?' + query : ''}`)
}

export async function assignTrainerFocusArea(data) {
  return request('/api/trainer-focus-areas', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function deleteTrainerFocusArea(id) {
  return request(`/api/trainer-focus-areas/${id}`, { method: 'DELETE' })
}

// Target Groups (Zielgruppen)
export async function listTargetGroups(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/target-groups${query ? '?' + query : ''}`)
}

export async function createTargetGroup(data) {
  return request('/api/target-groups', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateTargetGroup(id, data) {
  return request(`/api/target-groups/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteTargetGroup(id) {
  return request(`/api/target-groups/${id}`, { method: 'DELETE' })
}

// Style Direction → Target Groups (M:N Assignments)
export async function listStyleDirectionTargetGroups(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/training-style-target-groups${query ? '?' + query : ''}`)
}

export async function createStyleDirectionTargetGroup(data) {
  return request('/api/training-style-target-groups', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateStyleDirectionTargetGroup(id, data) {
  return request(`/api/training-style-target-groups/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteStyleDirectionTargetGroup(id) {
  return request(`/api/training-style-target-groups/${id}`, { method: 'DELETE' })
}

export async function getStyleDirectionsHierarchy(filters = {}) {
  const query = new URLSearchParams(filters).toString()
  return request(`/api/training-styles/hierarchy${query ? '?' + query : ''}`)
}

// Trainer Contexts (Fokussierte Ansichten)
export async function listTrainerContexts() {
  return request('/api/trainer-contexts')
}

export async function createTrainerContext(data) {
  return request('/api/trainer-contexts', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

export async function updateTrainerContext(id, data) {
  return request(`/api/trainer-contexts/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  })
}

export async function deleteTrainerContext(id) {
  return request(`/api/trainer-contexts/${id}`, { method: 'DELETE' })
}

// ============================================================================
// Version & Health
// ============================================================================

export async function getVersion() {
  return request('/api/version')
}

export async function healthCheck() {
  return request('/health')
}

export const api = {
  // Auth
  login,
  register,
  logout,
  getCurrentProfile,
  listProfiles,
  listAdminUsers,
  updateProfile,
  managementPasswordReset,
  changePassword,
  verifyEmail,
  resendVerification,

  // Clubs & Groups
  listClubs,
  getClub,
  createClub,
  updateClub,
  deleteClub,
  listClubMembers,
  getClubMember,
  addClubMember,
  updateClubMember,
  removeClubMember,
  listPublicClubsDirectory,
  clubMembersDirectory,
  getMyClubJoinRequests,
  createClubJoinRequest,
  withdrawClubJoinRequest,
  listClubJoinRequests,
  acceptClubJoinRequest,
  rejectClubJoinRequest,
  getInboxJoinRequests,
  listDivisions,
  createDivision,
  updateDivision,
  deleteDivision,
  listTrainingGroups,
  getTrainingGroup,
  createTrainingGroup,
  updateTrainingGroup,
  deleteTrainingGroup,

  // Skills & Methods
  listSkills,
  listSkillsCatalog,
  getSkill,
  createSkill,
  updateSkill,
  deleteSkill,
  listMethods,
  getMethod,
  createMethod,
  updateMethod,
  deleteMethod,

  // Exercises + Medien/Archiv (Progression, KI) → frontend/src/api/exercises.js
  ...exercises,

  // Training Planning → frontend/src/api/planning.js
  ...planning,

  // Catalogs
  listFocusAreas,
  createFocusArea,
  updateFocusArea,
  deleteFocusArea,
  getAdminHierarchy,
  listStyleDirections,
  listTrainingStyles,
  createStyleDirection,
  updateStyleDirection,
  deleteStyleDirection,
  listTrainingCharacters,
  createTrainingCharacter,
  updateTrainingCharacter,
  deleteTrainingCharacter,
  listTrainingTypes,
  createTrainingType,
  updateTrainingType,
  deleteTrainingType,
  listSkillMainCategories,
  createSkillMainCategory,
  updateSkillMainCategory,
  deleteSkillMainCategory,
  listSkillCategories,
  createSkillCategory,
  updateSkillCategory,
  deleteSkillCategory,
  listTrainerFocusAreas,
  assignTrainerFocusArea,
  deleteTrainerFocusArea,
  listTargetGroups,
  createTargetGroup,
  updateTargetGroup,
  deleteTargetGroup,
  listStyleDirectionTargetGroups,
  createStyleDirectionTargetGroup,
  updateStyleDirectionTargetGroup,
  deleteStyleDirectionTargetGroup,
  listMaturityModels,
  listMaturityModelContextBindings,
  upsertMaturityModelContextBinding,
  deleteMaturityModelContextBinding,
  importMaturityModelBundle,
  exportMaturityModelBundle,
  exportResolvedMaturityBundle,
  exportMatrixStackBundle,
  importMatrixStackBundle,
  resolveMaturityModel,
  getMaturityModel,
  createMaturityModel,
  updateMaturityModel,
  deleteMaturityModel,
  addMaturityModelSkill,
  removeMaturityModelSkill,
  upsertMaturityModelSkillLevels,
  getStyleDirectionsHierarchy,
  listTrainerContexts,
  createTrainerContext,
  updateTrainerContext,
  deleteTrainerContext,

  // System
  getVersion,
  healthCheck,

  // MediaWiki Import
  previewMediaWikiImport: (category, importType = 'exercise', limit = 10) =>
    request(`/api/import/mediawiki/preview?category=${encodeURIComponent(category)}&import_type=${importType}&limit=${limit}`),

  executeMediaWikiImport: (data) =>
    request('/api/import/mediawiki/execute', {
      method: 'POST',
      body: JSON.stringify(data)
    }),

  getMediaWikiImportStatus: (logId) =>
    request(`/api/import/mediawiki/status/${logId}`),

  listMediaWikiImportLogs: () =>
    request('/api/import/mediawiki/logs'),

  deleteMediaWikiImportReference: (refId) =>
    request(`/api/import/mediawiki/references/${refId}`, { method: 'DELETE' }),

  // Legal Documents (public)
  getPublishedLegalDocument: (documentType) =>
    request(`/api/legal-documents/${documentType}/published`),

  // Legal Documents (superadmin)
  listLegalDocuments: (documentType) =>
    request(`/api/admin/legal-documents${documentType ? `?document_type=${documentType}` : ''}`),
  createLegalDocument: (data) =>
    request('/api/admin/legal-documents', { method: 'POST', body: JSON.stringify(data) }),
  getLegalDocument: (id) =>
    request(`/api/admin/legal-documents/${id}`),
  updateLegalDocument: (id, data) =>
    request(`/api/admin/legal-documents/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
  publishLegalDocument: (id, changeNote) =>
    request(`/api/admin/legal-documents/${id}/publish`, {
      method: 'POST',
      body: JSON.stringify({ change_note: changeNote }),
    }),
  archiveLegalDocument: (id) =>
    request(`/api/admin/legal-documents/${id}/archive`, { method: 'POST' }),
  copyLegalDocumentAsDraft: (id) =>
    request(`/api/admin/legal-documents/${id}/copy-as-draft`, { method: 'POST' }),
  getLegalDocumentAudit: (id) =>
    request(`/api/admin/legal-documents/${id}/audit`),

  // P-13: Content-Melde-Backend
  submitContentReport: (body) =>
    request('/api/content-reports', { method: 'POST', body: JSON.stringify(body) }),
  getInboxContentReports: () =>
    request('/api/me/inbox/content-reports'),
  getContentReport: (id) =>
    request(`/api/content-reports/${id}`),
  patchContentReport: (id, body) =>
    request(`/api/content-reports/${id}`, { method: 'PATCH', body: JSON.stringify(body) }),
  setLegalHoldFromReport: (id, body) =>
    request(`/api/content-reports/${id}/legal-hold`, { method: 'POST', body: JSON.stringify(body) }),
}

export default api
