import { describe, expect, it, vi } from 'vitest';
import type { AlqoveClient } from '../../client';
import { createReturnEndpoints } from '../returns';
import { createReviewEndpoints } from '../reviews';
import { createMessageEndpoints } from '../messages';

function transport() {
  return { get: vi.fn(), post: vi.fn(), delete: vi.fn(), postFormData: vi.fn() };
}
describe('explicit seller store endpoints', () => {
  it('scopes message lists, writes, attachments and inbox to one store', () => {
    const http = transport();
    const messages = createMessageEndpoints(http as unknown as AlqoveClient).forStore('store-b');
    messages.threads();
    messages.list('order-a');
    messages.post('order-a', { body: 'hello' });
    messages.delete('message-a');
    const file = new File(['image'], 'image.png');
    messages.uploadAttachment('order-a', file);
    expect(http.get.mock.calls).toEqual([['/v1/stores/store-b/threads'], ['/v1/stores/store-b/orders/order-a/messages']]);
    expect(http.post).toHaveBeenCalledWith('/v1/stores/store-b/orders/order-a/messages', { body: 'hello' });
    expect(http.delete).toHaveBeenCalledWith('/v1/stores/store-b/messages/message-a');
    expect(http.postFormData.mock.calls[0][0]).toBe('/v1/stores/store-b/orders/order-a/messages/attachments');
    expect(http.postFormData.mock.calls[0][1].get('file')).toBe(file);
  });
  it('scopes private review listings and summary separately from public reviews', () => {
    const http = transport();
    const reviews = createReviewEndpoints(http as unknown as AlqoveClient);
    reviews.sellerReviews('store-b', { page: 2 });
    reviews.sellerRatingSummary('store-b');
    expect(http.get.mock.calls).toEqual([
      ['/v1/stores/store-b/seller/reviews?page=2'], ['/v1/stores/store-b/seller/rating-summary'],
    ]);
  });
  it('scopes every return read and action to the selected store, including nested orders', () => {
    const http = transport();
    const returns = createReturnEndpoints(http as unknown as AlqoveClient).forStore('store-b');
    returns.listSeller({ state: 'requested' });
    returns.get('return-a');
    returns.approve('return-a', 125);
    returns.reject('return-a', 'reason');
    returns.markReceived('return-a');
    returns.retryLabel('return-a');
    returns.sellerCloseWithoutRefund('return-a', 'reason');
    returns.proactive('order-a', { mode: 'keep-it', item_ids: ['item-a'] });
    expect(http.get.mock.calls).toEqual([
      ['/v1/stores/store-b/returns?state=requested'], ['/v1/stores/store-b/returns/return-a'],
    ]);
    expect(http.post.mock.calls).toEqual([
      ['/v1/stores/store-b/returns/return-a/approve', { restocking_fee_cents: 125 }],
      ['/v1/stores/store-b/returns/return-a/reject', { reason_text: 'reason' }],
      ['/v1/stores/store-b/returns/return-a/mark-received', {}],
      ['/v1/stores/store-b/returns/return-a/retry-label', {}],
      ['/v1/stores/store-b/returns/return-a/seller-close-without-refund', { reason: 'reason' }],
      ['/v1/stores/store-b/orders/order-a/returns/proactive', { mode: 'keep-it', item_ids: ['item-a'] }],
    ]);
  });
});
