const User = require('../models/User');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const secretKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZW1iZXJfaWQiOiJkZjE0OWM4Ni02Njc0LTRmODItYTE0Yi05OWFjNmZjYjJjNjIiLCJ1c2VybmFtZSI6ImNoYW5hY2hhaSIsImlhdCI6MTcyNDYzNDI0NSwiZXhwIjoxNzI0NjM3ODQ1fQ.bxa0LGelFhlGGb8PSgNd1oq-QZKqaNKbLA6oMuzEEeo'; // Replace with your own secret key
exports.login = async (req, res) => {
    try {
        const { member_username, member_password } = req.body;

        
        const member = await Member.findOne({ where: { member_username } });

        if (!member) {
            return res.status(404).json({ error: 'Member not found' });
        }

        
        const isMatch = await bcrypt.compare(member_password, member.member_password);

        if (!isMatch) {
            return res.status(401).json({ error: 'Invalid credentials' });
        }

        
        const token = jwt.sign(
            { member_id: member.member_id, username: member.username },
            secretKey,
            { expiresIn: '1y' } 
        );

        res.status(200).json({ accessToken : token });
    } catch (err) {
        res.status(500).json({ error: err.message });
    }

};