<?php
declare(strict_types=1);

// Christo Lochenberg Developments | info@christodev.co.za | +27 61 523 9126

class Partner {
    private Database $db;

    public function __construct() {
        $this->db = new Database();
    }

    public function getPartners(): array {
        $this->db->query('SELECT * FROM partners ORDER BY name ASC');
        return $this->db->resultSet();
    }

    public function getPartnerById(int $id): object|bool {
        $this->db->query('SELECT * FROM partners WHERE id = :id');
        $this->db->bind(':id', $id);
        return $this->db->single();
    }

    public function addPartner(array $data): bool {
        $this->db->query('INSERT INTO partners (name, email, contact_number, address_street, address_suburb, address_city, address_state, address_zip, address_country, logo, jobcard_prefix, jobcard_sequence) VALUES (:name, :email, :contact_number, :address_street, :address_suburb, :address_city, :address_state, :address_zip, :address_country, :logo, :jobcard_prefix, :jobcard_sequence)');
        
        $this->db->bind(':name', $data['name']);
        $this->db->bind(':email', $data['email']);
        $this->db->bind(':contact_number', $data['contact_number']);
        $this->db->bind(':address_street', $data['address_street']);
        $this->db->bind(':address_suburb', $data['address_suburb']);
        $this->db->bind(':address_city', $data['address_city']);
        $this->db->bind(':address_state', $data['address_state']);
        $this->db->bind(':address_zip', $data['address_zip']);
        $this->db->bind(':address_country', $data['address_country']);
        $this->db->bind(':logo', $data['logo']);
        $this->db->bind(':jobcard_prefix', $data['jobcard_prefix']);
        $this->db->bind(':jobcard_sequence', $data['jobcard_sequence']);
        
        return $this->db->execute();
    }

    public function addPartnerReturnId(array $data): int|bool {
        $this->db->query('INSERT INTO partners (name, email, contact_number, address_street, address_suburb, address_city, address_state, address_zip, address_country, logo, jobcard_prefix, jobcard_sequence) VALUES (:name, :email, :contact_number, :address_street, :address_suburb, :address_city, :address_state, :address_zip, :address_country, :logo, :jobcard_prefix, :jobcard_sequence)');
        
        $this->db->bind(':name', $data['name']);
        $this->db->bind(':email', $data['email']);
        $this->db->bind(':contact_number', $data['contact_number']);
        $this->db->bind(':address_street', $data['address_street']);
        $this->db->bind(':address_suburb', $data['address_suburb']);
        $this->db->bind(':address_city', $data['address_city']);
        $this->db->bind(':address_state', $data['address_state']);
        $this->db->bind(':address_zip', $data['address_zip']);
        $this->db->bind(':address_country', $data['address_country']);
        $this->db->bind(':logo', $data['logo']);
        $this->db->bind(':jobcard_prefix', $data['jobcard_prefix']);
        $this->db->bind(':jobcard_sequence', $data['jobcard_sequence']);
        
        if ($this->db->execute()) {
            $this->db->query('SELECT LAST_INSERT_ID() as id');
            $row = $this->db->single();
            return (int)$row->id;
        }
        return false;
    }

    public function updatePartner(array $data): bool {
        $this->db->query('UPDATE partners SET name = :name, email = :email, contact_number = :contact_number, address_street = :address_street, address_suburb = :address_suburb, address_city = :address_city, address_state = :address_state, address_zip = :address_zip, address_country = :address_country, logo = :logo, jobcard_prefix = :jobcard_prefix, jobcard_sequence = :jobcard_sequence WHERE id = :id');
        
        $this->db->bind(':id', $data['id']);
        $this->db->bind(':name', $data['name']);
        $this->db->bind(':email', $data['email']);
        $this->db->bind(':contact_number', $data['contact_number']);
        $this->db->bind(':address_street', $data['address_street']);
        $this->db->bind(':address_suburb', $data['address_suburb']);
        $this->db->bind(':address_city', $data['address_city']);
        $this->db->bind(':address_state', $data['address_state']);
        $this->db->bind(':address_zip', $data['address_zip']);
        $this->db->bind(':address_country', $data['address_country']);
        $this->db->bind(':logo', $data['logo']);
        $this->db->bind(':jobcard_prefix', $data['jobcard_prefix']);
        $this->db->bind(':jobcard_sequence', $data['jobcard_sequence']);

        return $this->db->execute();
    }

    public function deletePartner(int $id): bool {
        $this->db->query('DELETE FROM partners WHERE id = :id');
        $this->db->bind(':id', $id);
        return $this->db->execute();
    }
}