import type { Request } from "express";
import type { customers, staffUsers } from "../db/schema.js";

export type AuthActorType = "customer" | "staff";
export type StaffRole = "seller" | "admin";

export type Customer = typeof customers.$inferSelect;
export type StaffUser = typeof staffUsers.$inferSelect;

export type PublicCustomer = {
  id: number;
  email: string;
  name: string | null;
  phoneNumber: string | null;
  emailVerifiedAt: Date | null;
  createdAt: Date;
};

export type PublicStaffUser = {
  id: number;
  name: string;
  email: string;
  role: StaffRole;
  isActive: boolean;
  emailVerifiedAt: Date | null;
  createdAt: Date;
};

export type JwtAuthPayload = {
  sub: string;
  actorType: AuthActorType;
  role?: StaffRole;
};

export type AuthenticatedUser =
  | { actorType: "customer"; customer: PublicCustomer }
  | { actorType: "staff"; staff: PublicStaffUser };

export type AuthenticatedRequest = Request & {
  auth?: AuthenticatedUser;
};

export type AuthTokenResponse<TUser> = {
  accessToken: string;
  refreshToken: string;
  user: TUser;
};

export type RefreshTokenResponse = {
  accessToken: string;
  refreshToken: string;
};

export type OtpRequestResponse = {
  email: string;
  expiresInMinutes: number;
  delivery: "email" | "console";
  code?: string;
};
