import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ContentSubmission } from './content.entity';
import { Application } from '../applications/application.entity';
import axios from 'axios';

@Injectable()
export class TrackingService {
  constructor(
    @InjectRepository(ContentSubmission)
    private contentRepo: Repository<ContentSubmission>,
    @InjectRepository(Application)
    private applicationRepo: Repository<Application>,
  ) {}

  async submitLink(applicationId: string, url: string): Promise<ContentSubmission> {
    const app = await this.applicationRepo.findOne({
      where: { id: applicationId },
      relations: ['campaign'],
    });
    if (!app) throw new NotFoundException('Application not found');

    const sub = this.contentRepo.create({
      application: { id: applicationId },
      url,
      ai_verification_status: 'verifying'
    });
    const saved = await this.contentRepo.save(sub);

    // Trigger async AI verification
    this.verifySubmission(saved.id, app.campaign.title).catch(e => console.error("Verify err", e));

    return saved;
  }

  async verifySubmission(submissionId: string, campaignTitle: string) {
    const sub = await this.contentRepo.findOne({ where: { id: submissionId } });
    if (!sub) return;

    try {
      // Call the Python AI Engine
      const res = await axios.post('http://127.0.0.1:8000/analyze-url', {
        url: sub.url,
        campaign_keywords: campaignTitle.split(' ').filter(k => k.length > 3)
      });

      const data = res.data;
      sub.platform = data.platform;
      sub.metrics = data.metrics || {};
      sub.ai_notes = data.ai_notes;
      sub.ai_verification_status = data.status || 'verified';
      await this.contentRepo.save(sub);
    } catch (error) {
      sub.ai_verification_status = 'pending';
      sub.ai_notes = 'Verification failed. Retrying later.';
      await this.contentRepo.save(sub);
    }
  }

  async getSubmissions(applicationId: string): Promise<ContentSubmission[]> {
    return this.contentRepo.find({
      where: { application: { id: applicationId } },
      order: { created_at: 'DESC' },
    });
  }

  async getSubmissionsForBrand(brandId: string): Promise<ContentSubmission[]> {
    return this.contentRepo
      .createQueryBuilder('cs')
      .leftJoinAndSelect('cs.application', 'app')
      .leftJoinAndSelect('app.campaign', 'campaign')
      .leftJoinAndSelect('app.creator', 'creator')
      .leftJoinAndSelect('creator.creatorProfile', 'profile')
      .where('campaign.brand.id = :brandId', { brandId })
      .orderBy('cs.created_at', 'DESC')
      .getMany();
  }

  async getSubmissionsForCampaign(campaignId: string): Promise<ContentSubmission[]> {
    return this.contentRepo
      .createQueryBuilder('cs')
      .leftJoinAndSelect('cs.application', 'app')
      .leftJoinAndSelect('app.campaign', 'campaign')
      .leftJoinAndSelect('app.creator', 'creator')
      .leftJoinAndSelect('creator.creatorProfile', 'profile')
      .where('campaign.id = :campaignId', { campaignId })
      .orderBy('cs.created_at', 'DESC')
      .getMany();
  }
}
