import * as v from "valibot";
import { deviceSchema } from "@domains/device/data";
import { supportedAppSchema } from "@domains/supported-app/data";

export enum LinkStatus {
    ACTIVE = "active",
    INACTIVE = "inactive",
    EXPIRED = "expired",
    REVOKED = "revoked",
}

export const linkStatusSchema = v.picklist(["active", "inactive", "expired", "revoked"]);
export type LinkStatusValue = v.InferOutput<typeof linkStatusSchema>;

export const linkSchema = v.object({
    id: v.number(),
    token: v.string(),
    status: linkStatusSchema,
    linkedDeviceId: v.nullable(v.number()),
    supportedAppId: v.number(),
    expiresAt: v.nullable(v.date()),
    lastAccessedAt: v.nullable(v.date()),
    createdAt: v.date(),
    updatedAt: v.date(),
});
export type Link = v.InferOutput<typeof linkSchema>;

export const linkWithDeviceSchema = v.object({
    ...linkSchema.entries,
    device: v.nullable(deviceSchema),
    supportedApp: v.nullable(supportedAppSchema),
});
export type LinkWithDevice = v.InferOutput<typeof linkWithDeviceSchema>;

export const createLinkSchema = v.object({
    token: v.pipe(v.string(), v.minLength(1)),
    linkedDeviceId: v.number(),
    supportedAppId: v.number(),
    expiresAt: v.optional(v.nullable(v.date())),
});
export type CreateLink = v.InferOutput<typeof createLinkSchema>;

export const updateLinkSchema = v.partial(
    v.object({
        status: linkStatusSchema,
        linkedDeviceId: v.nullable(v.number()),
        supportedAppId: v.number(),
        expiresAt: v.nullable(v.date()),
        lastAccessedAt: v.nullable(v.date()),
    }),
);
export type UpdateLink = v.InferOutput<typeof updateLinkSchema>;
