import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '@config/database';

interface SubscriptionAttributes {
  subscriptionId: string;
  userId: string;
  endpoint: string;
  p256dh: string;
  auth: string;
  userAgent?: string | null;
  createdAt: Date;
}

interface SubscriptionCreationAttributes extends Optional<SubscriptionAttributes, 'subscriptionId' | 'userAgent' | 'createdAt'> {}

class Subscription extends Model<SubscriptionAttributes, SubscriptionCreationAttributes> implements SubscriptionAttributes {
  public subscriptionId!: string;
  public userId!: string;
  public endpoint!: string;
  public p256dh!: string;
  public auth!: string;
  public userAgent!: string | null;
  public createdAt!: Date;
}

Subscription.init(
  {
    subscriptionId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'subscription_id'
    },
    userId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'user_id'
    },
    endpoint: {
      type: DataTypes.STRING(1000),
      allowNull: false
    },
    p256dh: {
      type: DataTypes.STRING(255),
      allowNull: false
    },
    auth: {
      type: DataTypes.STRING(255),
      allowNull: false
    },
    userAgent: {
      type: DataTypes.STRING(500),
      allowNull: true,
      field: 'user_agent'
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'created_at'
    }
  },
  {
    sequelize,
    modelName: 'Subscription',
    tableName: 'subscriptions',
    timestamps: false,
    indexes: [
      { fields: ['user_id'] },
      { unique: true, fields: ['endpoint'] }
    ]
  }
);

export { Subscription };


