const liffService = {
    liff: null,
    isLoggedIn: false,
    profile: null,

    async init(liffId) {
        if (!liffId) {
            console.warn('LIFF ID not configured');
            return false;
        }
        try {
            await liff.init({ liffId });
            this.liff = liff;
            this.isLoggedIn = liff.isLoggedIn();
            if (this.isLoggedIn) {
                this.profile = await liff.getProfile();
            }
            return true;
        } catch (error) {
            console.error('LIFF init error:', error);
            return false;
        }
    },

    login() {
        if (this.liff && !this.isLoggedIn) {
            this.liff.login();
        }
    },

    async getProfile() {
        if (this.liff && this.isLoggedIn) {
            return await this.liff.getProfile();
        }
        return null;
    },

    sendMessage(message) {
        if (this.liff && this.liff.isInClient()) {
            return this.liff.sendMessages([{ type: 'text', text: message }]);
        }
    },

    closeWindow() {
        if (this.liff && this.liff.isInClient()) {
            this.liff.closeWindow();
        }
    }
};
