"use client";

import { FormEvent, useState } from "react";
import { Copy, LinkIcon, Save, Send } from "lucide-react";
import { useRouter } from "next/navigation";

import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Checkbox } from "@/components/ui/checkbox";
import { Input } from "@/components/ui/input";
import { Table } from "@/components/ui/table";
import type { ApiResult } from "@/modules/core/server/api";
import type { Organization, OrganizationInvitation, RoleDefinition } from "@/modules/core/types";
import { getInviteHref, OrganizationInviteDialog } from "@/modules/settings/components/OrganizationInviteDialog";

type OrganizationDetailClientProps = {
  invitations: OrganizationInvitation[];
  organization: Organization;
  roles: RoleDefinition[];
};

export function OrganizationDetailClient({
  invitations,
  organization,
  roles,
}: OrganizationDetailClientProps): React.ReactElement {
  const router = useRouter();
  const [isInviteOpen, setIsInviteOpen] = useState(false);
  const [settingsMessage, setSettingsMessage] = useState("");
  const [isPending, setIsPending] = useState(false);

  async function handleSettingsSubmit(event: FormEvent<HTMLFormElement>): Promise<void> {
    event.preventDefault();
    setSettingsMessage("");
    setIsPending(true);
    const formData = new FormData(event.currentTarget);
    const response = await fetch(`/api/organizations/${organization.id}`, {
      method: "PATCH",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({
        registrationEnabled: formData.get("registrationEnabled") === "on",
        oidcEnabled: formData.get("oidcEnabled") === "on",
        oidcIssuerUrl: String(formData.get("oidcIssuerUrl") ?? ""),
        oidcClientId: String(formData.get("oidcClientId") ?? ""),
        oidcClientSecret: String(formData.get("oidcClientSecret") ?? ""),
        oidcScopes: String(formData.get("oidcScopes") ?? ""),
        oidcRedirectUri: String(formData.get("oidcRedirectUri") ?? ""),
        oidcAvatarClaim: String(formData.get("oidcAvatarClaim") ?? ""),
        oidcAutoProvision: formData.get("oidcAutoProvision") === "on",
      }),
    });
    const result = (await response.json()) as ApiResult<{ organization: Organization }>;
    setIsPending(false);

    if (!result.success) {
      setSettingsMessage(result.reason);
      return;
    }

    setSettingsMessage(result.reason);
    router.refresh();
  }

  return (
    <div className="grid gap-5 xl:grid-cols-[0.95fr_1.05fr]">
      <Card>
        <CardHeader>
          <CardTitle>机构访问配置</CardTitle>
          <CardDescription>控制公开注册、OIDC 登录入口和自动开户策略。</CardDescription>
        </CardHeader>
        <CardContent>
          <form className="grid gap-4" onSubmit={handleSettingsSubmit}>
            <div className="rounded-md border p-3 text-sm">
              <Checkbox
                controlFirst={false}
                defaultChecked={organization.registrationEnabled}
                label={
                  <span className="min-w-0">
                    <span className="block font-medium">开放公开注册</span>
                    <span className="block text-xs text-muted-foreground">关闭后，注册页不再展示该机构；邀请链接仍可使用。</span>
                  </span>
                }
                name="registrationEnabled"
              />
            </div>

            <div className="rounded-md border p-3 text-sm">
              <Checkbox
                controlFirst={false}
                defaultChecked={organization.oidcEnabled}
                label={
                  <span className="min-w-0">
                    <span className="block font-medium">启用 OIDC 登录</span>
                    <span className="block text-xs text-muted-foreground">保存标准 OIDC 配置，后续登录入口按该开关展示。</span>
                  </span>
                }
                name="oidcEnabled"
              />
            </div>

            <div className="grid gap-3 md:grid-cols-2">
              <Input
                defaultValue={organization.oidcIssuerUrl}
                label="Issuer URL"
                name="oidcIssuerUrl"
                placeholder="https://idp.example.com"
              />
              <Input defaultValue={organization.oidcClientId} label="Client ID" name="oidcClientId" />
            </div>

            <Input
              label="Client Secret"
              name="oidcClientSecret"
              placeholder={organization.oidcHasClientSecret ? "已配置，留空则不修改" : "未配置"}
              type="password"
            />

            <div className="grid gap-3 md:grid-cols-2">
              <Input
                defaultValue={organization.oidcScopes}
                label="Scopes"
                name="oidcScopes"
                placeholder="openid profile email"
              />
              <Input
                defaultValue={organization.oidcRedirectUri}
                label="Redirect URI"
                name="oidcRedirectUri"
                placeholder="/api/auth/oidc/callback"
              />
            </div>

            <Input
              defaultValue={organization.oidcAvatarClaim}
              label="Avatar Claim"
              name="oidcAvatarClaim"
              placeholder="picture"
            />

            <div className="rounded-md border p-3 text-sm">
              <Checkbox
                controlFirst={false}
                defaultChecked={organization.oidcAutoProvision}
                label={
                  <span className="min-w-0">
                    <span className="block font-medium">OIDC 自动开户</span>
                    <span className="block text-xs text-muted-foreground">首次 OIDC 登录时自动创建账号并进入本机构。</span>
                  </span>
                }
                name="oidcAutoProvision"
              />
            </div>

            {settingsMessage ? (
              <p className="rounded-md bg-secondary px-3 py-2 text-sm text-secondary-foreground" role="status">
                {settingsMessage}
              </p>
            ) : null}

            <Button className="w-fit" disabled={isPending} type="submit">
              <Save className="size-4" aria-hidden="true" />
              保存配置
            </Button>
          </form>
        </CardContent>
      </Card>

      <Card>
        <CardHeader className="gap-4 md:flex-row md:items-start md:justify-between">
          <div>
            <CardTitle>机构邀请</CardTitle>
            <CardDescription>生成邀请链接后发送给成员；可限定邮箱，也可生成通用邀请。</CardDescription>
          </div>
          <Button onClick={() => setIsInviteOpen(true)} type="button">
            <Send className="size-4" aria-hidden="true" />
            发起邀请
          </Button>
        </CardHeader>
        <CardContent className="grid gap-4">
          <div className="overflow-x-auto rounded-md border">
            <Table className="w-full min-w-[820px] text-sm">
              <Table.Header className="bg-secondary text-left text-xs text-muted-foreground">
                <Table.Row>
                  <Table.Head className="px-4 py-3 font-medium">对象</Table.Head>
                  <Table.Head className="px-4 py-3 font-medium">角色</Table.Head>
                  <Table.Head className="px-4 py-3 font-medium">状态</Table.Head>
                  <Table.Head className="px-4 py-3 font-medium">使用次数</Table.Head>
                  <Table.Head className="px-4 py-3 font-medium">有效期</Table.Head>
                  <Table.Head className="px-4 py-3 text-right font-medium">链接</Table.Head>
                </Table.Row>
              </Table.Header>
              <Table.Body className="divide-y bg-card">
                {invitations.map((invitation) => (
                  <Table.Row className="hover:bg-secondary/45" key={invitation.id}>
                    <Table.Cell className="px-4 py-3">{invitation.email || "通用邀请"}</Table.Cell>
                    <Table.Cell className="px-4 py-3">{invitation.roleLabel}</Table.Cell>
                    <Table.Cell className="px-4 py-3">
                      <Badge variant={invitation.status === "active" ? "success" : "secondary"}>{invitation.status}</Badge>
                    </Table.Cell>
                    <Table.Cell className="px-4 py-3 text-muted-foreground">
                      {invitation.usedCount} / {invitation.maxUses}
                    </Table.Cell>
                    <Table.Cell className="px-4 py-3 text-muted-foreground">
                      {new Date(invitation.expiresAt).toLocaleString("zh-CN")}
                    </Table.Cell>
                    <Table.Cell className="px-4 py-3 text-right">
                      <Button
                        onClick={() => void navigator.clipboard.writeText(getInviteHref(invitation.token))}
                        size="sm"
                        type="button"
                        variant="outline"
                      >
                        <Copy className="size-4" aria-hidden="true" />
                        复制
                      </Button>
                    </Table.Cell>
                  </Table.Row>
                ))}
                {invitations.length === 0 ? (
                  <Table.Row>
                    <Table.Cell className="px-4 py-8 text-center text-muted-foreground" colSpan={6}>
                      暂无邀请
                    </Table.Cell>
                  </Table.Row>
                ) : null}
              </Table.Body>
            </Table>
          </div>
          <div className="flex items-center gap-2 text-xs text-muted-foreground">
            <LinkIcon className="size-4" aria-hidden="true" />
            邀请链接使用 `/register?invite=...`，注册成功后计入使用次数并自动加入机构。
          </div>
        </CardContent>
      </Card>
      <OrganizationInviteDialog
        onClose={() => setIsInviteOpen(false)}
        open={isInviteOpen}
        organization={organization}
        roles={roles}
      />
    </div>
  );
}
