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

const HttpPageFetchLog = sequelize.define('HttpPageFetchLog', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
    },
    userId: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
            model: User,
            key: 'id',
        },
        onDelete: 'SET NULL',
        onUpdate: 'CASCADE',
    },
    fetchType: {
        type: DataTypes.STRING(64),
        allowNull: false,
        comment: 'z.B. leaguePage, clubInfoDisplay, regionMeetingFilter',
    },
    baseDomain: {
        type: DataTypes.STRING(255),
        allowNull: false,
        comment: 'z.B. httv.click-tt.de',
    },
    fullUrl: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
    association: {
        type: DataTypes.STRING(64),
        allowNull: true,
        comment: 'Verband (z.B. HeTTV, RTTV)',
    },
    championship: {
        type: DataTypes.STRING(128),
        allowNull: true,
        comment: 'Championship-Parameter (z.B. HTTV 25/26)',
    },
    clubIdParam: {
        type: DataTypes.STRING(64),
        allowNull: true,
        comment: 'Club-ID falls clubInfoDisplay',
    },
    requestMethod: {
        type: DataTypes.STRING(16),
        allowNull: true,
        comment: 'HTTP-Methode des ausgehenden Requests',
    },
    requestHeaders: {
        type: DataTypes.TEXT('long'),
        allowNull: true,
        comment: 'Gesendete Request-Header als JSON',
    },
    requestBody: {
        type: DataTypes.TEXT('long'),
        allowNull: true,
        comment: 'Gesendeter Request-Body im Originalformat',
    },
    httpStatus: {
        type: DataTypes.INTEGER,
        allowNull: true,
    },
    success: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
    },
    responseSnippet: {
        type: DataTypes.TEXT('long'),
        allowNull: true,
        comment: 'Gekürzter Response-Anfang zur Strukturanalyse',
    },
    contentType: {
        type: DataTypes.STRING(128),
        allowNull: true,
    },
    responseHeaders: {
        type: DataTypes.TEXT('long'),
        allowNull: true,
        comment: 'Empfangene Response-Header als JSON',
    },
    responseBody: {
        type: DataTypes.TEXT('long'),
        allowNull: true,
        comment: 'Vollstaendiger Response-Body',
    },
    responseUrl: {
        type: DataTypes.TEXT,
        allowNull: true,
        comment: 'Finale Response-URL nach Redirects',
    },
    errorMessage: {
        type: DataTypes.TEXT,
        allowNull: true,
    },
    executionTimeMs: {
        type: DataTypes.INTEGER,
        allowNull: true,
        comment: 'Laufzeit in Millisekunden',
    },
}, {
    underscored: true,
    tableName: 'http_page_fetch_log',
    timestamps: true,
    updatedAt: false,
    indexes: [
        { fields: ['created_at'] },
        { fields: ['base_domain', 'fetch_type'] },
        { fields: ['association', 'championship'] },
    ],
});

HttpPageFetchLog.belongsTo(User, { foreignKey: 'userId', as: 'user' });
User.hasMany(HttpPageFetchLog, { foreignKey: 'userId', as: 'httpPageFetchLogs' });

export default HttpPageFetchLog;
