import ContactModel from '../../models/Contact';
import { IContact, ICreateContactData } from '../../types/models';

export class HelpAndFeedbackRepository {

    // Create a new contact
    public static async createContact(contactData: ICreateContactData) {
        try {
            const contact = new ContactModel(contactData);
            return await contact.save();
        } catch (error) {
            throw error;
        }
    }

    // Get all contacts with optional filtering
    public static async getAllContacts(
        page: number = 1,
        limit: number = 10,
        filter: any = {}
    ) {
        try {
            const skip = (page - 1) * limit;
            const total = await ContactModel.countDocuments(filter);
            const contacts = await ContactModel
                .find(filter)
                .populate('user_id', 'first_name last_name email')
                .populate('qrcode_id', 'title type')
                .populate('scan_id', 'notes device_type')
                .sort({ createdAt: -1 })
                .skip(skip)
                .limit(limit)
                .lean();

            return {
                contacts,
                total,
                page,
                totalPages: Math.ceil(total / limit)
            };
        } catch (error) {
            throw error;
        }
    }

    // Get a single contact by ID
    public static async getContactById(id: string) {
        try {
            return await ContactModel
                .findById(id)
                .populate('user_id', 'first_name last_name email')
                .populate('qrcode_id', 'title type')
                .populate('scan_id', 'notes device_type')
                .lean();
        } catch (error) {
            throw error;
        }
    }

    // Update a contact by ID
    public static async updateContact(id: string, updateData: Partial<IContact>) {
        try {
            return await ContactModel.findByIdAndUpdate(
                id,
                { ...updateData, updatedAt: new Date() },
                { new: true, runValidators: true }
            )
                .populate('user_id', 'first_name last_name email')
                .populate('qrcode_id', 'title type')
                .populate('scan_id', 'notes device_type')
                .lean();
        } catch (error) {
            throw error;
        }
    }

    // Delete a contact by ID
    public static async deleteContact(id: string) {
        try {
            const result = await ContactModel.findByIdAndDelete(id);
            return !!result;
        } catch (error) {
            throw error;
        }
    }

    // Update contact status
    public static async updateContactStatus(id: string, status: 'pending' | 'resolved' | 'closed') {
        try {
            return await ContactModel.findByIdAndUpdate(
                id,
                { contact_status: status, updatedAt: new Date() },
                { new: true, runValidators: true }
            )
                .populate('user_id', 'first_name last_name email')
                .populate('qrcode_id', 'title type')
                .populate('scan_id', 'notes device_type')
                .lean();
        } catch (error) {
            throw error;
        }
    }

    // Update contact priority
    public static async updateContactPriority(id: string, priority: 'high' | 'medium' | 'low') {
        try {
            return await ContactModel.findByIdAndUpdate(
                id,
                { contact_priority: priority, updatedAt: new Date() },
                { new: true, runValidators: true }
            )
                .populate('user_id', 'first_name last_name email')
                .populate('qrcode_id', 'title type')
                .populate('scan_id', 'notes device_type')
                .lean();
        } catch (error) {
            throw error;
        }
    }
}