import * as mongoose from "mongoose";
import { UserStatus } from "../enums/user-status.enum";
import { Role } from "../enums/roles.enum";
import { SessionLocation } from "../enums/session-location.enum";
import { UserType } from "../enums/user-types.enum";

export const UserSchema = new mongoose.Schema(
    {
        first_name: {
            type: String,
            required: [true, "First name is required"],
            trim: true,
            minlength: [2, "First name must be at least 2 characters long"],
            maxlength: [50, "First name cannot exceed 50 characters"],
        },
        last_name: {
            type: String,
            required: [true, "Last name is required"],
            trim: true,
            minlength: [2, "Last name must be at least 2 characters long"],
            maxlength: [50, "Last name cannot exceed 50 characters"],
        },
        email: {
            type: String,
            required: [true, "Email is required"],
            unique: true,
            match: [/^\S+@\S+\.\S+$/, "Email format is invalid"],
            lowercase: true,
            trim: true,
        },
        phone: {
            type: String,
            match: [/^\d{10,15}$/, "Phone number must be between 10 to 15 digits"],
        },
        picture: {
            type: String,
            match: [/^(http|https):\/\/[^\s$.?#].[^\s]*$/, "Invalid URL format"],
        },
        password: {
            type: String,
            required: [true, "Password is required"],
            minlength: [6, "Password must be at least 6 characters long"],
        },
        isAdmin: {
            type: Boolean,
            default: false,
        },
        hasShippingAddress: {
            type: Boolean,
            default: false,
        },
        status: {
            type: String,
            enum: {
                values: [UserStatus.ACTIVE, UserStatus.PENDING, UserStatus.SUSPENDED, UserStatus.CANCELLED],
                message: "Invalid status",
            },
            default: UserStatus.ACTIVE,
        },
        role: {
            type: String,
            enum: {
                values: [Role.ADMIN, Role.OWNER, Role.MANAGER, Role.USER, Role.GUEST],
                message: "Invalid role",
            },
            default: Role.USER,
        },
        refreshToken: {
            type: String,
        },
        balance: {
            type: Number,
            default: 0,
            min: [0, "Balance cannot be negative"],
        },
        tenantId: {
            type: String,
            required: [true, "Tenant ID is required"],
        },
        membership: {
            type: String,
            default: "basic",
            enum: {
                values: ["basic", "premium", "gold"],
                message: "Invalid membership type",
            },
        },
        billingAddress: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Address",
        },
        shippingAddress: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Address",
        },
        session_location: {
            type: String,
            enum: {
                values: [SessionLocation.ONLINE, SessionLocation.ONSITE],
                message: "Invalid session location",
            },
            default: SessionLocation.ONLINE,
        },
        parentId: {
            type: String,
            ref: "User",
        },
        rate: {
            type: Number,
            default: 0,
            min: [0, "Rate cannot be negative"],
        },
        userType: {
            type: String,
            enum: {
                values: [UserType.PARENT, UserType.TEACHER, UserType.CHILD],
                message: "Invalid user type",
            },
            default: UserType.PARENT,
        },
    },
    {
        timestamps: true,
        toJSON: { virtuals: true },
        toObject: { virtuals: true },
    },
);
