import {
    CreateLink,
    Link,
    LinkStatus,
    LinkWithDevice,
    UpdateLink,
} from "./data";
import { FlowExecCtx } from "@core/flow.execution.context";
import { errAsync, okAsync, ResultAsync } from "neverthrow";
import { LinkRepository } from "./repository";
import { type Err } from "@pkg/result";
import { linkErrors } from "./errors";
import { nanoid } from "nanoid";
import { db } from "@pkg/db";

export class LinkController {
    constructor(private repo: LinkRepository) {}

    list(fctx: FlowExecCtx): ResultAsync<LinkWithDevice[], Err> {
        return this.repo.list(fctx);
    }

    getById(fctx: FlowExecCtx, id: number): ResultAsync<LinkWithDevice, Err> {
        return this.repo.getById(fctx, id);
    }

    /**
     * Fetch a link by its URL token, including the joined device.
     * Used by apps/frontend to validate and resolve an incoming link.
     */
    getByToken(
        fctx: FlowExecCtx,
        token: string,
    ): ResultAsync<LinkWithDevice, Err> {
        return this.repo.getByToken(fctx, token);
    }

    /**
     * Validate a token: must exist, be active, and not be expired.
     * Returns the resolved link+device on success.
     */
    validate(
        fctx: FlowExecCtx,
        token: string,
    ): ResultAsync<LinkWithDevice, Err> {
        return this.repo.getByToken(fctx, token).andThen((l) => {
            if (l.status !== LinkStatus.ACTIVE) {
                return errAsync(linkErrors.linkNotActive(fctx, token));
            }
            if (l.expiresAt && l.expiresAt < new Date()) {
                return errAsync(linkErrors.linkExpired(fctx, token));
            }
            return this.repo.touch(fctx, token).map(() => l);
        });
    }

    /**
     * Validate a token without updating lastAccessedAt.
     * Useful for high-frequency polling paths.
     */
    validateReadOnly(
        fctx: FlowExecCtx,
        token: string,
    ): ResultAsync<LinkWithDevice, Err> {
        return this.repo.getByToken(fctx, token).andThen((l) => {
            if (l.status !== LinkStatus.ACTIVE) {
                return errAsync(linkErrors.linkNotActive(fctx, token));
            }
            if (l.expiresAt && l.expiresAt < new Date()) {
                return errAsync(linkErrors.linkExpired(fctx, token));
            }
            return okAsync(l);
        });
    }

    /**
     * Generate a new link. Token is auto-generated as a URL-safe nanoid.
     */
    create(
        fctx: FlowExecCtx,
        data: Omit<CreateLink, "token">,
    ): ResultAsync<Link, Err> {
        return this.repo.create(fctx, {
            ...data,
            token: nanoid(12),
        });
    }

    update(
        fctx: FlowExecCtx,
        id: number,
        data: UpdateLink,
    ): ResultAsync<Link, Err> {
        return this.repo.update(fctx, id, data);
    }

    assignDevice(
        fctx: FlowExecCtx,
        id: number,
        deviceId: number | null,
    ): ResultAsync<Link, Err> {
        return this.repo.update(fctx, id, { linkedDeviceId: deviceId });
    }

    revoke(fctx: FlowExecCtx, id: number): ResultAsync<Link, Err> {
        return this.repo.update(fctx, id, { status: LinkStatus.REVOKED });
    }

    delete(fctx: FlowExecCtx, id: number): ResultAsync<boolean, Err> {
        return this.repo.delete(fctx, id);
    }
}

export function getLinkController(): LinkController {
    return new LinkController(new LinkRepository(db));
}
