import { apiClient } from './client';

export interface ActiveSession {
    id: number;
    device: string;
    ip: string | null;
    at: string | null;
    current: boolean;
}

export interface AccountSecurity {
    last_password_change_at: string | null;
    sessions: ActiveSession[];
}

export interface ChangePasswordPayload {
    current_password: string;
    password: string;
    password_confirmation: string;
}

/** GET /api/account/security — дата последней смены пароля + недавние входы. */
export async function getAccountSecurity(): Promise<AccountSecurity> {
    const { data } = await apiClient.get<AccountSecurity>('/api/account/security');
    return data;
}

/** POST /api/account/change-password — смена пароля. Возвращает ISO-дату смены. */
export async function changePassword(payload: ChangePasswordPayload): Promise<string> {
    const { data } = await apiClient.post<{ last_password_change_at: string }>(
        '/api/account/change-password',
        payload,
    );
    return data.last_password_change_at;
}

/** DELETE /api/account/sessions/{id} — завершить (отозвать) сессию. */
export async function revokeSession(id: number): Promise<void> {
    await apiClient.delete(`/api/account/sessions/${id}`);
}
