import client from '../client';

// --- Types ---
export interface OAuthClient {
  id: string;
  client_id: string;
  client_name: string;
  scopes: string[];
  rate_limit_per_minute: number;
  is_active: boolean;
  token_lifetime_seconds: number;
  created_at: string;
  version: number;
}

export interface OAuthClientDetail extends OAuthClient {
  tenant_id: string;
  client_secret: string;
  allowed_patient_ids?: string[];
}

export interface CreateOAuthClientReq {
  client_name: string;
  scopes: string[];
  allowed_patient_ids?: string[];
  rate_limit_per_minute?: number;
  token_lifetime_seconds?: number;
}

export interface UpdateOAuthClientReq {
  client_name?: string;
  scopes?: string[];
  allowed_patient_ids?: string[] | null;
  rate_limit_per_minute?: number;
  is_active?: boolean;
  token_lifetime_seconds?: number;
  version: number;
}

export interface RegenerateSecretResp {
  client_id: string;
  client_secret: string;
}

// --- FHIR Scope ---
export const FHIR_SCOPE_OPTIONS = [
  { value: 'Patient.read', label: 'Patient.read — 读取患者' },
  { value: 'Observation.read', label: 'Observation.read — 读取体征' },
  { value: 'Device.read', label: 'Device.read — 读取设备' },
  { value: 'DiagnosticReport.read', label: 'DiagnosticReport.read — 读取诊断报告' },
  { value: 'Encounter.read', label: 'Encounter.read — 读取就诊记录' },
  { value: 'Practitioner.read', label: 'Practitioner.read — 读取医护' },
  { value: 'Appointment.read', label: 'Appointment.read — 读取预约' },
  { value: 'Task.read', label: 'Task.read — 读取随访任务' },
];

// --- API ---
export const oauthClientApi = {
  list: () =>
    client.get('/health/oauth/clients').then((r) => r.data.data as OAuthClient[]),

  create: (data: CreateOAuthClientReq) =>
    client.post('/health/oauth/clients', data).then((r) => r.data.data as OAuthClientDetail),

  update: (id: string, data: UpdateOAuthClientReq) =>
    client.put(`/health/oauth/clients/${id}`, data).then((r) => r.data.data as OAuthClient),

  delete: (id: string) =>
    client.delete(`/health/oauth/clients/${id}`).then((r) => r.data),

  regenerateSecret: (id: string) =>
    client.post(`/health/oauth/clients/${id}/regenerate-secret`).then((r) => r.data.data as RegenerateSecretResp),
};
