import GraphQLService from './GraphQLService';

export interface FeaturesData{
  id : number
  title : string
  desc : string 
}
export interface Features{
  id? : number
  title : string
  desc : string 
}
export interface FeaturesOPData {
  FeaturesFind?: {
      rows?:FeaturesData[]      
      total : number
      pages : number 
  };
}
export interface FeaturesIdOPData {
  FeaturesFindOneById?:FeaturesData
}
export interface FeaturesCreateOPData {
  FeaturesCreate?:FeaturesData[]
}

interface SnapshotData {
  message : string
  result : string
  imp : string
  mismatch : string
  notFound : string
  conflict : string
  success : string
  error : string
  alert : string
  warning : string
  notice : string
  info : string
}
export interface FeaturesUpdateOPData {
  FeaturesUpdate?:{
    snapshot: SnapshotData
    affected : number
    affectedRows ?: FeaturesData[]
  }
}
export interface FeaturesUpsertOPData {
  FeaturesUpsert?:FeaturesData
}
export interface FeaturesDeleteOPData {
  FeaturesDelete?:{
    snapshot: SnapshotData
    affected : number
  }
}

class FeatureService extends GraphQLService {
    constructor() {
      super();
    }
    getFeatures(pageSize:number, start:number,sortorder:string):Promise<FeaturesOPData |null | undefined>{
      const Query =`query Rows($filter: FeaturesFindInput!) {
                    FeaturesFind(filter: $filter) {
                      rows {
                        id
                        title
                        desc
                      }
                      total
                      pages
                    }
                  }`;
      const variables = {
          "filter": {
            "order": {
              "title": sortorder
            },
            "skip": start,
            "take": pageSize
          }
      };
  
      return this.execute(Query, variables); 
    }

    getAllFeatures():Promise<FeaturesOPData |null | undefined>{
      const Query =`query Rows($filter: FeaturesFindInput!) {
                    FeaturesFind(filter: $filter) {
                      rows {
                        id
                        title
                        desc
                      }
                      total
                      pages
                    }
                  }`;
      const variables = {
          "filter": {
            "order": {
              "id": "DESC"
            },
          }
      };
  
      return this.execute(Query, variables); 
    }

    getFeaturesById(Id: number):Promise<FeaturesIdOPData |null | undefined>{
      const Query =`query FeaturesFindOneById($input: FeaturesFindOneByIdInput!) {
                    FeaturesFindOneById(input: $input) {
                      id
                      title
                      desc
                    }
                  }`;
      const variables = {
          "input": {
            "id": Id
          }
      };
  
      return this.execute(Query, variables); 
    }

    createFeatures(FeaturesINData: Features):Promise<FeaturesCreateOPData |null | undefined>{
      const Query =`mutation FeaturesCreate($input: [FeaturesCreateInput!]!) {
                    FeaturesCreate(input: $input) {
                      id
                      title
                      desc
                    }
                  }`;
      const variables = {
        input:[FeaturesINData]
      };
  
      return this.mutate<FeaturesCreateOPData>(Query, variables); 
    }

    updateFeatures(FeaturesINData: FeaturesData):Promise<FeaturesUpdateOPData |null | undefined>{
      const Query =`mutation FeaturesUpdate($input: FeaturesUpdateInput!) {
                    FeaturesUpdate(input: $input) {
                      snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                      affected
                      affectedRows {
                        id
                        title
                        desc
                      }
                    }
                  }`;
            const variables = {
              "input": {
                "sets": {
                  "desc": FeaturesINData.desc,
                  "title": FeaturesINData.title
                },
                "where": [
                  {
                    "id": {
                      "equal": `${FeaturesINData.id}`
                    }
                  }
                ]
              }
            };
  
      return this.mutate<FeaturesUpdateOPData>(Query, variables); 
    }

    upsertFeatures(FeaturesINData: FeaturesData):Promise<FeaturesUpsertOPData |null | undefined>{
      const Query =`mutation FeaturesUpsert($input: [FeaturesUpsertInput!]!) {
                    FeaturesUpsert(input: $input) {
                      id
                      title
                      desc
                    }
                  }`;
            const variables = {
              input:[FeaturesINData]
            };
  
      return this.mutate<FeaturesUpsertOPData>(Query, variables); 
    }

    deleteFeatures(Id: number):Promise<FeaturesDeleteOPData |null | undefined>{
      const Query =`mutation FeaturesDelete($input: FeaturesDeleteInput!) {
                    FeaturesDelete(input: $input) {
                      snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                      affected
                    }
                  }`;
            const variables = {
              input:{
                "where": [
                  {
                    "id": {
                      "equal": `${Id}`
                    }
                  }
                ]
              }
            };
  
      return this.mutate<FeaturesDeleteOPData>(Query, variables); 
    }

    sortAscGetRoomType(sortOrder:string,pageSize:number, start:number):Promise<FeaturesOPData |null | undefined>{
      const Query =`query Rows($filter: FeaturesFindInput!) {
                    FeaturesFind(filter: $filter) {
                      rows {
                        id
                        title
                        desc
                      }
                      total
                      pages
                    }
                  }`;
      const variables = {
        "filter": {
          "order": {
            "title": sortOrder
          },
          "skip": start,
          "take": pageSize
        }
    };

      return this.execute(Query, variables); 
    }
}

const featureServiceInstance = new FeatureService();
export default featureServiceInstance;