import mongoose from 'mongoose';

import { MODEL_NAMES } from './_models';

export interface IBankIntegration {
  provider: string;
  accessToken: string;
  refreshToken: string;
  tokenType?: string;
  tokenId?: string;
  expiresAt: Date;
  scopes: string;
  connectedAt: Date;
  lastSyncAt: Date;
}

export interface IBankIntegrationDoc extends IBankIntegration, mongoose.Document {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IBankIntegrationLean extends IBankIntegration {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IBankIntegrationPlain extends IBankIntegration {
  _id: string;
  createdAt: Date;
  updatedAt: Date;
}

export type TBankIntegrationModel = mongoose.Model<IBankIntegration>;

export const BankIntegrationSchema = new mongoose.Schema<IBankIntegration, TBankIntegrationModel>(
  {
    provider: {
      type: String,
    },
    accessToken: {
      type: String,
    },
    refreshToken: {
      type: String,
    },
    tokenType: {
      type: String,
      required: false,
    },
    tokenId: {
      type: String,
      required: false,
    },
    expiresAt: {
      type: Date,
    },
    scopes: {
      type: String,
    },
    connectedAt: {
      type: Date,
    },
    lastSyncAt: {
      type: Date,
    },
  },
  { timestamps: true },
);

export const BankIntegration = mongoose.model<IBankIntegration, TBankIntegrationModel>(MODEL_NAMES.bankIntegrations, BankIntegrationSchema);
