import { expect } from "e2e";
import * as Effect from "effect/Effect";
import { CommandId, MessageId, ThreadId } from "@t3tools/contracts";
import { IOS, test } from "./t3-mobile.ts";
import { rpcClient, runPromise, environmentId } from "./t3.ts";
import { MOBILE, MOBILE_T3_HOME } from "../e2e.config.ts";

test("French attachment sizes in mobile context", IOS, async ({t3, app, screen, device}) => {
  const project = await t3.project("attachment-demo");
  const thread = await t3.thread({project, title:"Attachment language", messages:["Start"]});
  const origin = `http://127.0.0.1:${MOBILE.serverPort}`;
  await runPromise(Effect.gen(function*(){
    const client = yield* rpcClient(MOBILE_T3_HOME, origin);
    const input = {type:"file" as const, name:"notes-12k.txt", mimeType:"text/plain", sizeBytes:12288};
    const upload = yield* client["attachments.createUploadUrl"](input);
    yield* Effect.promise(async()=>{
      const response = await fetch(origin+upload.relativeUrl, {method:"POST", body:"x".repeat(12288), headers:{"Content-Type":"text/plain"}});
      if (!response.ok) throw new Error(`upload ${response.status}: ${await response.text()}`);
    });
    yield* client["orchestration.dispatchCommand"]({type:"message.dispatch",createdBy:"user",creationSource:"mobile",commandId:CommandId.make(crypto.randomUUID()),messageId:MessageId.make(crypto.randomUUID()),threadId:ThreadId.make(thread.threadId),text:"Inspect [notes-12k.txt](t3-context://v1/file/notes)",attachments:[{...input,id:upload.attachmentId}],context:{version:1,records:[{version:1,kind:"file",contextId:"notes",label:"notes-12k.txt",attachmentId:upload.attachmentId,name:input.name,mimeType:input.mimeType,sizeBytes:input.sizeBytes}]},dispatchMode:{type:"start_immediately"}});
  }).pipe(Effect.scoped));
  await t3.open();
  await device.openLink("t3code-dev://settings/language", {app:"com.t3tools.t3code.dev"});
  await screen.getByText("Français", {exact:true}).tap();
  await device.back();
  await device.back();
  await screen.getByText("Attachment language", {exact:false}).first().tap();
  await app.screenshot("sent-inline-context");
  await expect(screen.getByText("notes-12k.txt · 12 Ko", {exact:false})).toBeVisible();
});
