import { DataTypes } from 'sequelize';
import sequelize from '../database.js';
import User from './User.js';

const MyTischtennisFetchLog = sequelize.define('MyTischtennisFetchLog', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
    },
    userId: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
            model: User,
            key: 'id',
        },
        onDelete: 'CASCADE',
        onUpdate: 'CASCADE',
    },
    fetchType: {
        type: DataTypes.ENUM('ratings', 'match_results', 'league_table'),
        allowNull: false,
        comment: 'Type of data fetch: ratings, match_results, or league_table'
    },
    success: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
    },
    message: {
        type: DataTypes.TEXT,
        allowNull: true,
    },
    errorDetails: {
        type: DataTypes.TEXT,
        allowNull: true,
    },
    recordsProcessed: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: 'Number of records processed (e.g., players updated, matches fetched)'
    },
    executionTime: {
        type: DataTypes.INTEGER,
        allowNull: true,
        comment: 'Execution time in milliseconds'
    },
    isAutomatic: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
        comment: 'Whether this was an automatic or manual fetch'
    },
}, {
    underscored: true,
    tableName: 'my_tischtennis_fetch_log',
    timestamps: true,
    indexes: [
        {
            fields: ['user_id', 'fetch_type', 'created_at']
        },
        {
            fields: ['created_at']
        }
    ]
});

export default MyTischtennisFetchLog;

