<?php
namespace TCB\DB;
use TCB\Installer;
if ( ! defined( 'ABSPATH' ) ) { exit; }

class PartnersDAO {
    public static function get_all(): array {
        global $wpdb;
        return $wpdb->get_results( "SELECT * FROM " . Installer::t_partners() . " ORDER BY name ASC", ARRAY_A ) ?: [];
    }
    public static function get_by_id( int $id ): ?array {
        global $wpdb;
        return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . Installer::t_partners() . " WHERE id=%d", $id ), ARRAY_A ) ?: null;
    }
    public static function get_by_slug( string $slug ): ?array {
        global $wpdb;
        return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . Installer::t_partners() . " WHERE slug=%s", sanitize_title( $slug ) ), ARRAY_A ) ?: null;
    }
    public static function get_by_email( string $email ): ?array {
        global $wpdb;
        return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . Installer::t_partners() . " WHERE email=%s", $email ), ARRAY_A ) ?: null;
    }
    public static function get_by_user_id( int $user_id ): ?array {
        global $wpdb;
        return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . Installer::t_partners() . " WHERE wp_user_id=%d", $user_id ), ARRAY_A ) ?: null;
    }
    public static function insert( array $d ) {
        global $wpdb;
        $name = sanitize_text_field( $d['name'] ?? '' );
        $slug = sanitize_title( $d['slug'] ?? $name );
        if ( ! $slug ) $slug = sanitize_title( $name );
        $r = $wpdb->insert( Installer::t_partners(), [
            'name'         => $name,
            'slug'         => $slug,
            'company_url'  => esc_url_raw( $d['company_url'] ?? '' ),
            'logo_url'     => esc_url_raw( $d['logo_url'] ?? '' ),
            'email'        => sanitize_email( $d['email'] ?? '' ),
            'bio'          => wp_kses_post( $d['bio'] ?? '' ),
            'color'        => sanitize_hex_color( $d['color'] ?? '#e61a84' ) ?: '#e61a84',
            'booking_open' => isset( $d['booking_open'] ) ? (int) !! $d['booking_open'] : 1,
            'wp_user_id'   => (int)( $d['wp_user_id'] ?? 0 ),
            'status'       => in_array( $d['status'] ?? '', ['active','inactive'], true ) ? $d['status'] : 'active',
        ], [ '%s','%s','%s','%s','%s','%s','%s','%d','%d','%s' ] );
        return $r ? (int)$wpdb->insert_id : false;
    }
    public static function update( int $id, array $d ): bool {
        global $wpdb;
        $data = [];
        if ( isset( $d['name'] ) )         $data['name'] = sanitize_text_field( $d['name'] );
        if ( isset( $d['slug'] ) )         $data['slug'] = sanitize_title( $d['slug'] );
        if ( isset( $d['company_url'] ) )  $data['company_url'] = esc_url_raw( $d['company_url'] );
        if ( isset( $d['logo_url'] ) )     $data['logo_url'] = esc_url_raw( $d['logo_url'] );
        if ( isset( $d['email'] ) )        $data['email'] = sanitize_email( $d['email'] );
        if ( isset( $d['bio'] ) )          $data['bio'] = wp_kses_post( $d['bio'] );
        if ( isset( $d['color'] ) )        $data['color'] = sanitize_hex_color( $d['color'] ) ?: '#e61a84';
        if ( isset( $d['booking_open'] ) ) $data['booking_open'] = (int) !! $d['booking_open'];
        if ( isset( $d['wp_user_id'] ) )   $data['wp_user_id'] = (int)$d['wp_user_id'];
        if ( isset( $d['status'] ) )       $data['status'] = in_array( $d['status'], ['active','inactive'], true ) ? $d['status'] : 'active';
        if ( empty( $data ) ) return true;
        return $wpdb->update( Installer::t_partners(), $data, [ 'id' => $id ] ) !== false;
    }
    public static function delete( int $id ): bool {
        global $wpdb;
        return $wpdb->delete( Installer::t_partners(), [ 'id' => $id ], [ '%d' ] ) !== false;
    }

    public static function store_encrypted_password( int $id, string $encrypted ): bool {
        global $wpdb;
        return false !== $wpdb->update( Installer::t_partners(), [ 'encrypted_password' => $encrypted ], [ 'id' => $id ], [ '%s' ], [ '%d' ] );
    }

    public static function mark_credentials_sent( int $id ): bool {
        global $wpdb;
        return false !== $wpdb->update( Installer::t_partners(), [ 'credentials_sent' => 1, 'credentials_sent_at' => current_time( 'mysql' ) ], [ 'id' => $id ], [ '%d','%s' ], [ '%d' ] );
    }
}

