from django.db import models

class Program(models.Model):
    patient_name = models.CharField(max_length=255)
    patient_email = models.EmailField()
    clinician_name = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"Program for {self.patient_name} by {self.clinician_name}"

class Exercise(models.Model):

    program = models.ForeignKey(Program, on_delete=models.CASCADE, related_name='exercises')

    title = models.CharField(max_length=200)

    description = models.TextField()

    video_url = models.URLField(blank=True, null=True)



    def __str__(self):

        return self.title



    def get_video_id(self):

        if self.video_url:

            if 'youtube.com/watch' in self.video_url:

                return self.video_url.split('v=')[1].split('&')[0]

            elif 'youtu.be/' in self.video_url:

                return self.video_url.split('/')[-1]

        return None



    def get_video_thumbnail(self):

        video_id = self.get_video_id()

        if video_id:

            return f'https://img.youtube.com/vi/{video_id}/default.jpg'

        return None
